开发者

How to prevent the access to login page after successful login in Django?

开发者 https://www.devze.com 2023-02-24 18:23 出处:网络
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

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.

  1. Create a middleware
  2. Check if the current url matches your login url
  3. 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号