[Answer]-Django return successful/failure after processing POST data

1👍

You can use the Django messages framework

Quite commonly in web applications, you need to display a one-time
notification message (also known as “flash message”) to the user after
processing a form or some other types of user input.

https://docs.djangoproject.com/en/1.7/ref/contrib/messages/

Alternatively, you can add an extra variable to your template and display errors in the page.

def test_page(request):
    if request.method == "POST":
        try:
            # form-processing code
        except ValueError:
            # go back to previous page and show errors
            return render(request, 'previous_page.html',
                {'errors'=['Problem!']})

    # this will be rendered when above exception is not encountered
    return render_to_response('interface/test.html',\
                    context_instance=RequestContext(request))

And in the template (previous_page.html), you can do this:

{% for err in errors %}
    <div class="error-box">
        { err }
    </div>
{% endfor %}

Where the error-box class highlights your error messages as dismiss-able notifications or however you want to show it.

👤Kedar

Leave a comment