Search code examples
djangodjango-viewsdjango-formwizard

Django FormWizard and Permissions


I was wondering if there is a way to use permission decorators for Django's FormWizard. I know there is a way to do it via the urlconf, but I would like to avoid this and have my permissions all set up via the views.

I tried to override MyFormWizard.as_view() and add the decorators there, but then realised that as_view() is a @classonlymethod.

I don't have a lot of experience with Class Based Views and was wondering if there is an easy way to add the permission decorator on one of the FormWizard's methods? Any clues?


Solution

  • You don't have to decorate the view in the url conf. You can do so in your views.py,

    protected_wizard_view = login_required(MyWizardView.as_view())
    

    and then import protected_wizard_view in your urls.py.

    (r'^wizard/', protected_wizard_view),
    

    Another other option is to decorate the dispatch method, as described in the docs.

    class MyWizardView(WizardView):
    
        @method_decorator(login_required)
        def dispatch(self, *args, **kwargs):
            return super(MyWizardView, self).dispatch(*args, **kwargs)