Executing validation on entry to class-based views

22 views
Skip to first unread message

Darren Spruell

unread,
Sep 26, 2013, 2:29:38 AM9/26/13
to django...@googlegroups.com
Greetings,

I have an app with a CBV for the main functionality that I'd like to
do some validation on config settings (in settings.py) before carrying
out the view.

My particular view in this case is a FormWizard, but I'd like to know
general to any CBV; what is the correct pattern for doing this? Is
there a standard way to execute validation code upon entering a view
that allows one to access the request object (e.g. for setting
messages) and redirecting clients?

My initial thought was to override __init__() and perform it there but
IIANM it lacks self.request at that point.

What I'd like to implement:

if not settings.SOMEAPP_SETTINGS.get('sender_address'):
messages.error(self.request, "Configuration error:
submission sender address not found. Configure SOMEAPP_SETTINGS in
your project settings file.")
return redirect('index')

--
Darren Spruell
phatb...@gmail.com

Daniel Roseman

unread,
Sep 26, 2013, 4:24:33 AM9/26/13
to django...@googlegroups.com
One way would be to override `dispatch()`.  You have access to the request there and can choose whether to call the super method to do the actual dispatching, or redirect instead.
--
DR.

Darren Spruell

unread,
Sep 30, 2013, 2:00:43 AM9/30/13
to django...@googlegroups.com
Hm, yes! Thanks, had forgotten about dispatch().
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Darren Spruell
phatb...@gmail.com
Reply all
Reply to author
Forward
0 new messages