Sunday, September 29, 2013

Re: Executing validation on entry to class-based views

Hm, yes! Thanks, had forgotten about dispatch().

On Thu, Sep 26, 2013 at 1:24 AM, Daniel Roseman <daniel@roseman.org.uk> wrote:
> On Thursday, 26 September 2013 07:29:38 UTC+1, dspruell wrote:
>>
>> 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
>
>
>
> 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.
>
> --
> 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+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@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
phatbuckett@gmail.com

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKVSOJWnEG_GfonWfA%2BjUAwJ1_AfM8iRaQTLV_9XTSLCYw4mOg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment