[Answer]-Django: How to add errors messages next to the form

1👍

hm you use form and write all htmls in the html file why???

why don’t you use

views.py

if request.method=='POST':
    borrower = BorrowerForm(request.POST)
    if borrower.is_valid():
        ......
        return HttpResponseRedirect(<success url>)
    else:
        #here error messages it passed along with the form
        return render(request, 'books/clerk/add_borrower.html',
            {'borrower':borrower})

borrower = BorrowerForm()
return render(request, 'books/clerk/add_borrower.html',
   {'logged_in':logged_in, 'username':username, 'type':type, 
   'error':error, 'borrower':borrower})

and use it the templates. that is what the purpose of forms.py. like :

<form class="form-signin" method="post" action="/sign_in/" role="form">
    {% csrf_token %}
    {{borrower.as_table}}
</form>

Leave a comment