Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango: Search form in Class Based ListView
    text
    copied!<p>I am trying to realize a <code>Class Based ListView</code> which displays a selection of a table set. If the site is requested the first time, the dataset should be displayed. I would prefer a POST submission, but GET is also fine.</p> <p>That is a problem, which was easy to handle with <code>function based views</code>, however with class based views I have a hard time to get my head around.</p> <p>My problem is that I get a various number of error, which are caused by my limited understanding of the classed based views. I have read various documentations and I understand views for direct query requests, but as soon as I would like to add a form to the query statement, I run into different error. For the code below, I receive an <code>ValueError: Cannot use None as a query value</code>.</p> <p><strong>What would be the best practise work flow for a class based ListView depending on form entries (otherwise selecting the whole database)?</strong></p> <p>This is my sample code:</p> <p><strong>models.py</strong></p> <pre><code>class Profile(models.Model): name = models.CharField(_('Name'), max_length=255) def __unicode__(self): return '%name' % {'name': self.name} @staticmethod def get_queryset(params): date_created = params.get('date_created') keyword = params.get('keyword') qset = Q(pk__gt = 0) if keyword: qset &amp;= Q(title__icontains = keyword) if date_created: qset &amp;= Q(date_created__gte = date_created) return qset </code></pre> <p><strong>forms.py</strong></p> <pre><code>class ProfileSearchForm(forms.Form): name = forms.CharField(required=False) </code></pre> <p><strong>views.py</strong></p> <pre><code>class ProfileList(ListView): model = Profile form_class = ProfileSearchForm context_object_name = 'profiles' template_name = 'pages/profile/list_profiles.html' profiles = [] def post(self, request, *args, **kwargs): self.show_results = False self.object_list = self.get_queryset() form = form_class(self.request.POST or None) if form.is_valid(): self.show_results = True self.profiles = Profile.objects.filter(name__icontains=form.cleaned_data['name']) else: self.profiles = Profile.objects.all() return self.render_to_response(self.get_context_data(object_list=self.object_list, form=form)) def get_context_data(self, **kwargs): context = super(ProfileList, self).get_context_data(**kwargs) if not self.profiles: self.profiles = Profile.objects.all() context.update({ 'profiles': self.profiles }) return context </code></pre> <p>Below I added the FBV which does the job. <strong>How can I translate this functionality into a CBV?</strong> It seems to be so simple in function based views, but not in class based views.</p> <pre><code>def list_profiles(request): form_class = ProfileSearchForm model = Profile template_name = 'pages/profile/list_profiles.html' paginate_by = 10 form = form_class(request.POST or None) if form.is_valid(): profile_list = model.objects.filter(name__icontains=form.cleaned_data['name']) else: profile_list = model.objects.all() paginator = Paginator(profile_list, 10) # Show 10 contacts per page page = request.GET.get('page') try: profiles = paginator.page(page) except PageNotAnInteger: profiles = paginator.page(1) except EmptyPage: profiles = paginator.page(paginator.num_pages) return render_to_response(template_name, {'form': form, 'profiles': suppliers,}, context_instance=RequestContext(request)) </code></pre>
 

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