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?

    1 answer 1

    The problem was not in the code, the code works. If more, then due to my oversight, views.py was sloped into the next directory, and I worked with the wrong views.py. Thank you all interested!

    After editing the output style, list.html acquired the following form:

     {% block actions %} {% if perms.document.add %} <li> <a href="{% url 'archive-new' %}">Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ Π΄ΠΎΠΊΡƒΠΌΠ΅Π½Ρ‚</a> </li> <br /> {% endif %} <form action="" method="post">{% csrf_token %} <br /> {{ search_form.search_string.label }} {{ search_form.search_string }} {{ search_form.top_level.label }} {{ search_form.top_level }} <br /> <fieldset> <br /> {{ search_form.doc_types.label }} <br /> {{ search_form.doc_types }} <br /> <li><input type="submit" value="Найти" class="search"></li> </fieldset> </form> {% endblock %} 

    Now the lines are displayed in turn, and the Checkboxes in one line. It was not possible to display each Checkbox on a new line.

    • Please describe the complete solution to the problem and tick yourself, this is how StackOverflow works, maybe your experience will be useful for the rest - Ivan Semochkin
    • I will be able to accept the answer in 22 hours - this is how they write when you click on the check mark. - Mae