sgarza62
sgarza62

Reputation: 6238

Django: How to raise an exception when a user doesn't fill in a form at all?

How can I raise an exception when a user hits the "Submit" button on a registration form, without having filled in any of the fields on a form?


In the event that this would happen, I only want to display that single error ("Please fill out the form before submitting."), without displaying the field errors that would typically be displayed if a user forgets to fill in a required field.

In other words, if a user hits the submit button without filling in a single field, I want it to display one error only: "Please fill out the field before submitting."


I figure there might be two ways to achieve this:

The problem with the first option is that I would need to use an if statement, like the one below. But I don't know how to single out that particular error.

{% if form.special_error %}
...display the special error
...disregard any other field or non_field errors
{% endif %}

I only know how to use form.errors, form.non_field_errors, and form.<specific_field> in if statements. None of these would specifically indicate that this special error occurred.

There has to be an easy way to do this!!!

Upvotes: 2

Views: 669

Answers (1)

Raunak Agarwal
Raunak Agarwal

Reputation: 7238

If that's the case you could just check on the server side if the form has changed instead of validating it. By doing this:

def submit_form(request):
   if request.method == 'POST':
         form = Form(request.POST)
         if not form.has_changed():
             #Generate Error 

Upvotes: 3

Related Questions