Form validation in view class
Last updated
Was this helpful?
Last updated
Was this helpful?
In the class view, implement form_valid(self, form)
method
File : book/views.py
If invalid, set form.errors[]
and
return self.form_invalid(form)
The tricky part is how to show in the template.
File : book/templates/book.author_form.html
The validation messages are generic and allow to take more than one.
In order to display them, customized filter dict_key
is applied.
dict_key was learned from .
File : book/templatetags/dict_key.py
``` python from django import template from django.template.defaultfilters import register
register = template.Library()
@register.filter(name='dict_key') def dict_key(d, k): '''Returns the given key from a dictionary.''' return d[k]
```