tags:

views:

80

answers:

1

I'm developing a site with a login box on the base.html template page. For all the pages that require the user to be logged in, I'm using the login_required decorator. However, I'd like the user to be able to login on the other pages if they want to.

Among the things that the "login_required" decorator does is handle the nuts and bolts of logging in, in addition to verifying you're logged in before showing you the desired page.

What I'd like is a "login_optional" decorator (or optional parameter within login_required) for these optional pages. I can't find such a thing. Does it exist and I'm just missing it or do I need to roll my own login handling routine for these views?

Thanks

Mike
EDIT (sample code)
template

<form action="" method="post">
    <label for="username">Username:</label>
    <input type="text" name="username" value="" id="username" size="10"/>
    <label for="password">Password:</label>
    <input type="password" name="password" value="" id="password" size="10"/>
    <input type="submit" value="Login"/>
    <input type="hidden" name="next" value="{{ next|escape }}"/>
</form

>

login required code

@login_required
def chooseDistributor(request):
    user = request.user
    # blah stuff deleted
    # NOTHING TO HANDLE request.POST!
    return render_to_response('choose_distributor.html',locals())

login optional code

def showWelcome(request):
    user = request.user
    return render_to_response('welcome/home.html',locals())
+2  A: 

What do you mean? login_optional is just a view without a login_required decorator... :p.

Note that the login_required decorator does not handle any login functionality, it just checks if a user is logged in by controlling the User object of the request context. If he/she isn't logged in, this decorator redirects to a page that holds the real login functionality.

So the decorator is just a check + redirect, making login optional for a page hence is just removing the decorator.

KillianDS
See EDIT above. In short, I assume the login_required decorator handles the POST response from the login form. And, of course, a page without that decorator and without any other POST handling code will not be able to handle the login action. While I could simply copy the appropriate code from the login_required decorator, this violates the DRY principle and so I ask my question.
jamida
no, the login decorator does NOT contain that login POST handling code, only the login page does that. However, any well written login page will redirect you back to the view with the login decorator. Really, the login_required decorator does NOT contain any login handling code :p.
KillianDS
Yea, I looked and understand now... >>blush<< Thanks.
jamida
Just set the action param of your form to the login URL and it will work.
Guillaume Esquevin