Django Class-based Forms

Diving into Django’s class-based forms and ran into a common problem with reverse(). As documented here the form takes three parameters – template name, form_class and success_url.

My first instinct was to do the following:

class Form(FormView):
template_name = 'template.html'
form_class = FormClassName
success_url = reverse('my_url_name')

view raw
forms.py
hosted with ❤ by GitHub

This blows up rather nicely. The solution is rather simple – reverse_lazy():

class Form(FormView):
template_name = 'template.html'
form_class = FormClassName
success_url = reverse_lazy('my_url_name')

view raw
forms.py
hosted with ❤ by GitHub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s