Django: Redirect logged in users from login page

前端 未结 9 2405
醉酒成梦
醉酒成梦 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:33

    Since class based views (CBVs) is on the rise. This approach will help you redirect to another url when accessing view for non authenticated users only.

    In my example the sign-up page overriding the dispatch() method.

    class Signup(CreateView):
        template_name = 'sign-up.html'
    
        def dispatch(self, *args, **kwargs):
            if self.request.user.is_authenticated:
                return redirect('path/to/desired/url')
            return super().dispatch(*args, **kwargs)
    

    Cheers!

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

    anonymous_required decorator

    For class based views

    Code:

    from django.shortcuts import redirect
    
    def anonymous_required(func):
        def as_view(request, *args, **kwargs):
            redirect_to = kwargs.get('next', settings.LOGIN_REDIRECT_URL )
            if request.user.is_authenticated():
                return redirect(redirect_to)
            response = func(request, *args, **kwargs)
            return response
        return as_view
    

    Usage:

    url(r'^/?$',
       anonymous_required(auth_views.login),
    ),
    url(r'^register/?$',
        anonymous_required(RegistrationView.as_view()),
        name='auth.views.register'
    ),
    # Could be used to decorate the dispatch function of the view instead of the url
    

    For view functions

    From http://blog.motane.lu/2010/01/06/django-anonymous_required-decorator/

    Code:

    from django.http import HttpResponseRedirect
    
    def anonymous_required( view_function, redirect_to = None ):
        return AnonymousRequired( view_function, redirect_to )
    
    class AnonymousRequired( object ):
        def __init__( self, view_function, redirect_to ):
            if redirect_to is None:
                from django.conf import settings
                redirect_to = settings.LOGIN_REDIRECT_URL
            self.view_function = view_function
            self.redirect_to = redirect_to
    
        def __call__( self, request, *args, **kwargs ):
            if request.user is not None and request.user.is_authenticated():
                return HttpResponseRedirect( self.redirect_to )
            return self.view_function( request, *args, **kwargs )
    

    Usage:

    @anonymous_required
    def my_view( request ):
        return render_to_response( 'my-view.html' )
    
    0 讨论(0)
  • 2020-11-29 18:38

    All you have to do is set the "root" url to the homepage view. Since the homepage view is already restricted for logged on users, it'll automatically redirect anonymous users to the login page.

    Kepp the url as it is. And add something like:

    (r'^$', 'my_project.my_app.views.homepage'),
    
    0 讨论(0)
提交回复
热议问题