Hi folks, i want to build a searchform (with GET parameters) and also want to use pagination. But i can't find an easy solution for this common case. My solution has a searchform, a view and a template. Can anybody tell me if this is a good solution? I'm not sure because i find it very complicated for such an common case. maybe there's a much faster (easier) way to handle this.
forms.py class ProjectDataSearchForm(forms.Form): """ A Search form for the Project Data """ dtf = forms.DateTimeField(label='Datetime from', required=False) dtt = forms.DateTimeField(label='Datetime to', required=False) d_u = forms.BooleanField(label='Data with status unknown', required=False) d_g = forms.BooleanField(label='Data with status good', required=False) d_w = forms.BooleanField(label='Data with status warning', required=False) d_e = forms.BooleanField(label='Data with status error', required=False) views.py def project_data_list(request, project_id): """ Show a List with all Measurement Data from the Project """ #Get Project. Needed by template project = get_object_or_404(Project, pk=project_id, auth_group__in=request.user.groups.all()) #Use none-query query = Measurand.objects.none() newurl = '' if len(request.GET) > 0: form = ProjectDataSearchForm(data=request.GET) if form.is_valid(): #Standard query query = Measurand.objects.filter (entry__project=project_id, entry__project__auth_group__in=request.user.groups.all()) searchdict = form.cleaned_data qdict = { 'dtf':'entry__datetime__gte', 'dtt':'entry__datetime__lte', } q_objs = [Q(**{qdict[k]: searchdict[k]}) for k in qdict.keys() if searchdict.get(k, None)] query = query.filter(*q_objs) #get data status status = [] if searchdict['d_u']: status.append('U') if searchdict['d_g']: status.append('G') if searchdict['d_w']: status.append('W') if searchdict['d_e']: status.append('E') query = query.filter(status__in=status) rawurl = urlencode(form.data) if len(rawurl): newurl = '&' + rawurl query = query.order_by('-entry__datetime') else: form = ProjectDataSearchForm(initial={'d_u':True, 'd_g':True, 'd_w':True, 'd_e':True}) response = list_detail.object_list( request, queryset = query, template_name = 'project_data_list.html', template_object_name = 'measurand', paginate_by = 25, page = int(request.GET.get('page', '1')), extra_context = {'project':project, 'form':form, 'newurl':newurl} ) return response template.html {% extends 'base.html' %} {% load i18n %} {% block content %} <h1>{% trans "Browse Measurands" %} ({% trans "Project:" %} {{project.name}})</h1> <span class="searchform"> <form action="." method="GET"> <table> {{ form.as_table }} </table> <input type="submit" value="{% trans "Search" %}"> </form> </span> {% if is_paginated %} <div class="pagination"> <span class="step-links"> Select page: {% for p in page_range %} <a href=?page={{p}} {{newurl}}>{{p}}</a> {% endfor %} </span> </div> {% endif %} ........here is more stuff to print the data ................ {% endblock content %} --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---