Note that there are some explanatory texts on larger screens.

plurals
  1. POShowing a empty GET form without raising any error on Django
    primarykey
    data
    text
    <p>I'm trying to set an home page one my project that show a search form (with GET method). The problem is that it raises an error at the moment I load the very page since the default request method is GET. Of course I can change the form's method to POST but that would not be the correct way, so I was wondering if there was any condition that I can set to avoid this issue and still be able to check any errors.</p> <p>I'm working on <code>Django 1.5</code> and <code>Python 2.7</code></p> <p>This if the form class:</p> <pre><code>class Search(forms.Form): middleschool = 'MS' highschool = 'HS' university = 'U' blank = '-' school_choices = ((middleschool, 'Middle School'), (highschool, 'High school'), (university, 'University'), (blank, 'Not defined'),) title = forms.CharField(label='Keyworld') subject = forms.ModelChoiceField(queryset=Subject.objects.order_by('?'), required=False, label='Whitch subject you want to search?') school = forms.ChoiceField(choices = school_choices, required=False, label='What level of material are you searching?') price = forms.BooleanField(required=False) </code></pre> <p>This is the relative view:</p> <pre><code>def home(request): if request.method == 'GET': form = Search(request.GET) if form.is_valid(): cd = form.cleaned_data ftitle = cd['title'] fsubject = cd['subject'] fschool = cd['school'] fprice = cd['price'] if fprice: forms = File.objects.filter(name__contains='ftitle', subject='fsubject', school='fschool', price = '0,0') return render(request, 'search.html', {'form': form}) else: forms = File.objects.filter(name__contains='ftitle', subject='fsubject', school='fschool') return render(request, 'search.html', {'form': form}) else: form = Search() return render(request, 'home.html', {'form': form}) </code></pre> <p>This is the HTML:</p> <pre><code>{% block content %} &lt;hr&gt; &lt;div id='search'&gt; {% if form.errors %} &lt;p style="color: red;"&gt; Please correct the error{{ form.errors|pluralize }} below. &lt;/p&gt; {% endif %} &lt;form action="/search/" method="get"&gt; &lt;div class="field"&gt;{{ form.title.errors }}&lt;label for="keyworld"&gt;Keyworld:&lt;/label&gt;{{ form.title }}&lt;/div&gt; &lt;div class="field"&gt;&lt;label for="subject"&gt;Subject:&lt;/label&gt;{{ form.subject }}&lt;/div&gt; &lt;div class="field"&gt;&lt;label for="school"&gt;Level:&lt;/label&gt;{{ form.school }}&lt;/div&gt; &lt;div class="field"&gt;&lt;label for="price"&gt;Price yes or no:&lt;/label&gt;{{ form.price }}&lt;/div&gt; &lt;input type="submit" value="Search"&gt; &lt;input type="reset" value="Reset"&gt; &lt;/form&gt; &lt;/div&gt; &lt;hr&gt; {% endblock %} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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