Search code examples
pythondjangodjango-formsdjango-authentication

How do I combine Django's ‘LoginRequiredMixin’ and Form Tools' ‘FormPreview’?


LoginRequiredMixin works great on other class based views. Also the Form Tools FormPreview works fine. But when I try to use both together, then the LoginRequiredMixin gets ignored.

This is even the case if I use the most basic example from form tools and the Django documentation.

Two things I found strange (but this does not necessary be the reason):

  • "This mixin should be at the leftmost position in the inheritance list." This sounds like there is some internal hack that breaks if someone does something irregular. There is no explanation why.
  • The Form Tools view should be called itself and not the method .as_view(), which is off standard again.

Is this a bug? What can I do?


Solution

  • FormPreview is not a generic class based view, so you can't use it with mixins like LoginRequiredMixin.

    You can use the login_required decorator when you include the form preview instance in your URL patterns:

    from django.contrib.auth.decorators import login_required
    
    url_patterns = [
        url(r'^form-handler/$', login_required(MyFormPreview(SomeModelForm))),
    ]