Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think your goal is trying to filter queryset based on form submission, if so, by using GET : </p> <pre><code>class ProfileSearchView(ListView) template_name = '/your/template.html' model = Person def get_queryset(self): try: name = self.kwargs['name'] except: name = '' if (name != ''): object_list = self.model.objects.filter(name__icontains = name) else: object_list = self.model.objects.all() return object_list </code></pre> <p>Then all you need to do is write a <code>get</code> method to render template and context.</p> <p>Maybe not the best approach. By using the code above, you no need define a django form.</p> <p>Here's how it works : Class based views separates its way to render template, to process form and so on. Like, <code>get</code> handles GET response, <code>post</code> handles POST response, <code>get_queryset</code> and <code>get_object</code> is self explanatory, and so on. The easy way to know what's method available, fire up a shell and type :</p> <p><code>from django.views.generic import ListView</code> if you want to know about <code>ListView</code></p> <p>and then type <code>dir(ListView)</code>. There you can see all the method defined and go visit the source code to understand it. The <code>get_queryset</code> method used to get a queryset. Why not just define it like this, it works too : </p> <pre><code>class FooView(ListView): template_name = 'foo.html' queryset = Photo.objects.all() # or anything </code></pre> <p>We can do it like above, but we can't do dynamic filtering by using that approach. By using <code>get_queryset</code> we can do dynamic filtering, using any data/value/information we hav, it means we also can use <code>name</code> parameter that is sent by <code>GET</code>, and its available on <code>kwargs</code>, or in this case, on <code>self.kwargs["some_key"]</code> where <code>some_key</code> is any parameter you specified</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload