Good day. There are two models in the project: Document (number, name, date, type (foreign key), comment) and Document types (identifier, type). The page on the site displays a list of all documents and the search form: a text field where the search is by number, name or comment. My task is to add a group of Checkboxes to the search form β a list of document types so that you can sort the search results.
models.py.
class DocumentType(models.Model): globalid = models.CharField(max_length=38, unique=True, editable=False, default=braced_uuid, verbose_name='GlobalID') type = models.CharField(max_length=250, blank=False, null=False, db_index=True, verbose_name='ΡΠΈΠΏ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°') def __unicode__(self): return self.type class Meta: db_table = u'document_type' verbose_name = 'ΡΠΈΠΏ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°' verbose_name_plural = 'ΡΠΈΠΏΡ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠΎΠ²' class Document(models.Model): doc_no = models.CharField(max_length=30, blank=True, db_index=True, verbose_name='Π½ΠΎΠΌΠ΅Ρ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°') doc_date = models.DateField(max_length=30, blank=True, null=True, db_index=True, verbose_name='Π΄Π°ΡΠ° Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°') doc_type = models.ForeignKey(DocumentType, to_field='globalid', default = 'Π’ΠΈΠΏ Π½Π΅ Π²ΡΠ±ΡΠ°Π½', verbose_name='ΡΠΈΠΏ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°') doc_name = models.CharField(max_length=250, blank=False, db_index=True, verbose_name='Π½Π°Π·Π²Π°Π½ΠΈΠ΅ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΠ°') comments = models.CharField(max_length=250, blank=True, db_index=True, verbose_name='ΠΏΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅') def __unicode__(self): return self.doc_name class Meta: db_table = u'document' verbose_name = 'Π΄ΠΎΠΊΡΠΌΠ΅Π½Ρ' verbose_name_plural = 'Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΡ'
forms.py
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django import forms from archive.models import Document, DocumentType type_list = [x.type for x in DocumentType.objects.all()] class SearchForm(forms.Form): search_string = forms.CharField(required=False, label="ΠΠΎΠΈΡΠΊ") top_level = forms.BooleanField(required=False, initial=True, label="Π’ΠΎΠ»ΡΠΊΠΎ Π΄ΠΎΠΊΡΠΌΠ΅Π½ΡΡ Π²Π΅ΡΡ
Π½Π΅Π³ΠΎ ΡΡΠΎΠ²Π½Ρ") doc_types = forms.MultipleChoiceField(choices=type_list, required=False, initial=True, widget=forms.CheckboxSelectMultiple())
views.py
@login_required def list(request): # Search conditions can be obtained from the search form (POST data) # or from GET parameter. if request.method == 'POST': search_form = SearchForm(request.POST) if search_form.is_valid(): search = search_form.cleaned_data['search_string'] top_level = search_form.cleaned_data['top_level'] doc_types = search_form.cleaned_data['doc_types'] else: search = request.GET.get('search', '') top_level = request.GET.get('top_level', 'true').lower() != 'false' doc_types = request.GET.get('doc_types', 'true').lower() != 'false' search_form = SearchForm( {'search_string': search, 'top_level': top_level, 'doc_types': doc_types}) # We must have at least one match for each search word. # Some fields require exact match, some have to contain the word. if search: search_words = search.split() doc_list = Document.objects for word in search_words: doc_list = doc_list.filter( Q(doc_name__icontains=word) | Q(doc_tags__icontains=word) | Q(doc_no__icontains=word) | Q(archive_no__icontains=word) | Q(doc_group__icontains=word) | Q(address__icontains=word) | Q(comments__icontains=word) ) doc_list = doc_list.order_by('doc_name') else: doc_list = Document.objects.order_by('doc_name') if top_level: doc_list = doc_list.filter(Q(parent__isnull=True) | Q(parent='')) context = { 'search_form': search_form, # Django form for document searching 'search': search, # Space separated search keywords 'top_level': top_level, # Search among top-level documents only 'doc_types': doc_types, # Search only checked types of all documents 'documents': documents, # Paginator page } return render(request, 'archive/list.html', context)
list.html
{% block actions %} {% if perms.document.add %} <li><a href="{% url 'archive-new' %}">ΠΠΎΠ±Π°Π²ΠΈΡΡ Π΄ΠΎΠΊΡΠΌΠ΅Π½Ρ</a></li> {% endif %} <form action="" method="post">{% csrf_token %} <fieldset> {{ search_form.as_ul }} <li><input type="submit" value="ΠΠ°ΠΉΡΠΈ" class="search"></li><br /> </fieldset> <br /> </form> {% endblock %}
When loading the page, an error occurs in the line "{{search_form.as_ul}}" from list.html - what many do not like to the debugger?