Form validation in view class
In the class view, implement
form_valid(self, form)
methodFile :
book/views.py
If invalid, set
form.errors[]
andreturn 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 stackoverflow.
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]
```
Last updated