How to prevent the access to login page after successful login in Django?
Using the django.contrib.auth.views.login
method to perform the sign in method in my project, how to prevent the access to the /registration/login/
page again after s开发者_开发问答uccessful login?
You could deocrate your login-view with this AnonymousRequired-Decorator.
See this blog post for some background information on decorators which even explains things using your specific problem : http://passingcuriosity.com/2009/writing-view-decorators-for-django/
In your login view, check if a user is already authenticated:
def your_login(request):
if request.user.is_authenticated():
return redirect('yourhomepage') # or somewhere else or last url
EDIT
Assuming you want to keep authenticating that way, the only way I can think of redirecting is through a middleware.
- Create a middleware
- Check if the current url matches your login url
- If user is already authenticated, redirect to home page, otherwise process as usual
For check In your login view, check if a user is already authenticated or no you should take it:
def your_login(request):
if request.user.is_authenticated():
return redirect('/') #redirect to your home page
It is correct
Thanks for confirming my answer
精彩评论