Higure
Higure

Reputation: 235

Showing a empty GET form without raising any error on Django

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.

I'm working on Django 1.5 and Python 2.7

This if the form class:

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)

This is the relative view:

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})

This is the HTML:

{% block content %}
<hr>
<div id='search'>
    {% if form.errors %}
    <p style="color: red;">
        Please correct the error{{ form.errors|pluralize }} below.
    </p>
    {% endif %}

    <form action="/search/" method="get">
        <div class="field">{{ form.title.errors }}<label for="keyworld">Keyworld:</label>{{ form.title }}</div>
        <div class="field"><label for="subject">Subject:</label>{{ form.subject }}</div>
        <div class="field"><label for="school">Level:</label>{{ form.school }}</div>
        <div class="field"><label for="price">Price yes or no:</label>{{ form.price }}</div>
        <input type="submit" value="Search">
        <input type="reset" value="Reset">
    </form>
</div>
<hr>
{% endblock %}

Upvotes: 2

Views: 816

Answers (2)

niekas
niekas

Reputation: 9107

You simply have to add or None, like this:

form = SearchForm(request.GET or None)

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599856

Rather than checking the request method, you could check whether request.GET is empty:

if request.GET:
    form = SearchForm(request.GET)

Upvotes: 1

Related Questions