Phillip
Phillip

Reputation: 335

If views catches error, reload page and display error

I don't know if this is possible with Django or not, but here's my question.

I have a form where users submit some stuff, and I want to know if I can catch an error in my views and then reload the page using the original template and throw the error in there, so the user can see it?

I have some code that returns the error I want to display, but is it possible to redirect to the original template of my view and insert this error code into there IF the form fails?

Bear in mind that roundup isnt value a user submits, but a value that is calculated from some other python code.

if roundup >= 1: return HttpResponse("Price too low!")

Upvotes: 0

Views: 461

Answers (1)

scafrs
scafrs

Reputation: 453

You could add the error to your form in this way

if roundup >= 1:
     form.add_error(None, "Price too low!")
     return render(request, 'your_template', {'form': form})

and to show it the error in your template

{% for error in form.non_field_errors %}
     <div class='alert alert-danger' role='alert'>
         {{ error }}
    </div>
{% endfor %}

Upvotes: 3

Related Questions