Ben Ben - 1 month ago 6x
Python Question

Django: Tweaking @login_required decorator

I want to begin a private Beta for my website. I have a splash page where a user can enter a code to then access the rest of the site. Currently, all the other site pages (except the splash page) consist of a series of redirects set up by requiring user login (via @login_required decorator).

I want both logged in users and people who enter the Beta Tester code to be able to access the rest of the site. That means that I can't just use the decorator for all my views.

Should I alter the @login_required decorator itself? I'm more tempted to just do the following (I added a session variable if user enters correct code on splash page).

def view_name(request):
if not or not request.session.get('code_success'):
return HttpResponseRedirect('/splash/')

Does this seem reasonable? I'd hate to have to repeat it for all my views



Write your own decorator - it's fairly straight forward. In fact, if you look at the Django source for login_required, you should be able to fiddle around with a copy for your own purposes.

def my_login_required(function):
    def wrapper(request, *args, **kw):
        if not ( and request.session.get('code_success')):
            return HttpResponseRedirect('/splash/')
            return function(request, *args, **kw)
    return wrapper