Django: Redirect logged in users from login page

前端 未结 9 2404
醉酒成梦
醉酒成梦 2020-11-29 18:02

I want to set up my site so that if a user hits the /login page and they are already logged in, it will redirect them to the homepage. If they are not logged in

相关标签:
9条回答
  • 2020-11-29 18:18

    For Django 2.x, in your urls.py:

    from django.contrib.auth import views as auth_views
    from django.urls import path
    
    urlpatterns = [
        path('login/', auth_views.LoginView.as_view(redirect_authenticated_user=True), name='login'),
    ]
    
    0 讨论(0)
  • 2020-11-29 18:21

    I'm assuming you're currently using the built-in login view, with

    (r'^accounts/login/$', 'django.contrib.auth.views.login'),
    

    or something similar in your urls.

    You can write your own login view that wraps the default one. It will check if the user is already logged in and redirect if he is, and use the default view otherwise.

    something like:

    from django.contrib.auth.views import login
    
    def custom_login(request):
        if request.user.is_authenticated():
            return HttpResponseRedirect(...)
        else:
            return login(request)
    

    and of course change your urls accordingly:

    (r'^accounts/login/$', custom_login),
    
    0 讨论(0)
  • 2020-11-29 18:21

    I know this is a pretty old question, but I'll add my technique in case anyone else needs it:


    myproject/myapp/views/misc.py

    from django.contrib.auth.views import login as contrib_login, logout as contrib_logout
    from django.shortcuts import redirect
    from django.conf import settings
    
    
    def login(request, **kwargs):
        if request.user.is_authenticated():
            return redirect(settings.LOGIN_REDIRECT_URL)
        else:
            return contrib_login(request, **kwargs)
    
    logout = contrib_logout
    

    myproject/myapp/urls.py

    from django.conf.urls import patterns, url
    
    urlpatterns = patterns('myapp.views.misc',
        url(r'^login/$', 'login', {'template_name': 'myapp/login.html'}, name='login'),
        url(r'^logout/$', 'logout', {'template_name': 'myapp/logout.html'}, name='logout'),
    )
    ...
    
    0 讨论(0)
  • 2020-11-29 18:25

    Assuming that you are done setting up built-in Django user authentication (and using decorators), add this in your settings.py:

    LOGIN_REDIRECT_URL = '/welcome/'

    NOTE: '/welcome/' here is the URL of the homepage. It is up to you what to replace it with.

    0 讨论(0)
  • 2020-11-29 18:27

    The Django 1.10 way

    For Django 1.10, released in August 2016, a new parameter named redirect_authenticated_user was added to the login() function based view present in django.contrib.auth [1].

    Example

    Suppose we have a Django application with a file named views.py and another file named urls.py. The urls.py file will contain some Python code like this:

    #
    # Django 1.10 way
    #
    from django.contrib.auth import views as auth_views
    from . import views as app_views
    
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^login/', auth_views.login, name='login',
            kwargs={'redirect_authenticated_user': True}),
        url(r'^dashboard/', app_views.Dashboard.as_view(), name='dashboard'),
        url(r'^$', TemplateView.as_view(template_name='index.html'), name='index'),
    ]
    

    From that file, the relevant part within the urlpatterns variable definition is the following, which uses the already mentioned redirect_authenticated_user parameter with a True value:

        url(r'^login/', auth_views.login, name='login',
            kwargs={'redirect_authenticated_user': True}),
    

    Take note that the default value of the redirect_authenticated_user parameter is False.

    The Django 1.11 way

    For Django 1.11, released in April 2017, the LoginView class based view superseded the login() function based view [2], which gives you two options to choose from:

    • Use the same Django 1.10 way just described before, which is a positive thing because your current code will continue working fine. If you tell Python interpreter to display warnings, by for example running in a console terminal the command python -Wd manage.py runserver in your Django project directory and then going with a web browser to your login page, you would see in that same console terminal a warning message like this:

    /usr/local/lib/python3.6/site-packages/django/contrib/auth/views.py:54: RemovedInDjango21Warning: The login() view is superseded by the class-based LoginView().

    • Use the new Django 1.11 way, which will make your code more modern and compatible with future Django releases. With this option, the example given before will now look like the following one:

    Example

    We again suppose that we have a Django application with a file named views.py and another file named urls.py. The urls.py file will contain some Python code like this:

    #
    # Django 1.11 way
    #
    from django.contrib.auth import views as auth_views
    from . import views as app_views
    
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^login/',
            auth_views.LoginView.as_view(redirect_authenticated_user=True),
            name='login'),
        url(r'^dashboard/', app_views.Dashboard.as_view(), name='dashboard'),
        url(r'^$', TemplateView.as_view(template_name='index.html'), name='index'),
    ]
    

    From that file, the relevant part within the urlpatterns variable definition is the following, which again uses the already mentioned redirect_authenticated_user parameter with a True value, but passing it as an argument to the as_view method of the LoginView class:

        url(r'^login/',
            auth_views.LoginView.as_view(redirect_authenticated_user=False),
            name='login'),
    

    Take note that here the default value of the redirect_authenticated_user parameter is also False.

    References

    • [1] Relevant section in Django 1.10 release notes at https://docs.djangoproject.com/en/dev/releases/1.10/#django-contrib-auth
    • [2] Relevant section in Django 1.11 release notes at https://docs.djangoproject.com/en/1.11/releases/1.11/#django-contrib-auth
    0 讨论(0)
  • 2020-11-29 18:30

    Add this decorator above your login view to redirect to /home if a user is already logged in

    @user_passes_test(lambda user: not user.username, login_url='/home', redirect_field_name=None)

    and don't forget to import the decorator

    from django.contrib.auth.decorators import user_passes_test

    0 讨论(0)
提交回复
热议问题