Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The easiest way is to add the <s>login_required</s> user_passes_test decorator in the urlconf:</p> <pre><code>from django.conf.urls import patterns from django.contrib.auth.decorators import user_passes_test from .views import BusinessWizard urlpatterns = patterns('', (r'^wizard/$', user_passes_test(lambda user: user.is_anonymous(), 'some url', None)(BusinessWizard.as_view())), ) </code></pre> <p>Reference:<br> <a href="https://docs.djangoproject.com/en/dev/topics/class-based-views/#decorating-in-urlconf" rel="nofollow">https://docs.djangoproject.com/en/dev/topics/class-based-views/#decorating-in-urlconf</a><br> <a href="https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.decorators.user_passes_test" rel="nofollow">https://docs.djangoproject.com/en/dev/topics/auth/default/#django.contrib.auth.decorators.user_passes_test</a></p> <p>If you want to define your own decorator you can use the following snippet:</p> <pre><code>from functools import wraps from django.conf.urls import patterns from django.http import HttpResponseRedirect from .views import BusinessWizard def anonymous_required(redirect_url=None): """ Decorator that redirects authenticated users to a different URL. """ def decorator(view_func): @wraps(view_func, assigned=available_attrs(view_func)) def _wrapped_view(request, *args, **kwargs): if request.user.is_anonymous(): return view_func(request, *args, **kwargs) return HttpResponseRedirect(redirect_url) return _wrapped_view return decorator urlpatterns = patterns('', (r'^wizard/$', anonymous_required('some url')(BusinessWizard.as_view())), ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload