Django, redirect all non-authenticated users to landing page

前端 未结 8 1375
挽巷
挽巷 2021-01-30 23:08

I have a django website with many urls and views. Now I have asked to redirect all non-authenticated users to a certain landing page. So, all views must check if user.is_a

相关标签:
8条回答
  • 2021-01-30 23:49
    from django.contrib.auth.decorators import login_required
    
    @login_required(login_url='/login/')
    
    def home(request):
        return render(request, "home.html")
    

    It's showing like this: http://127.0.0.1:1235/login/?next=/home/

    0 讨论(0)
  • 2021-01-30 23:52

    see the docs for login required decorator

    from django.contrib.auth.decorators import login_required
    
    @login_required
    def my_view(request):
        ...
    

    another option is to add it to your urls.py patterns, see this answer

    urlpatterns = patterns('',
        (r'^foo/$', login_required(direct_to_template), {'template': 'foo_index.html'}),
    )
    
    0 讨论(0)
  • 2021-01-30 23:55

    You can use Middleware.

    Something like this will check user auth every request:

    class AuthRequiredMiddleware(object):
        def process_request(self, request):
            if not request.user.is_authenticated():
                return HttpResponseRedirect(reverse('landing_page')) # or http response
            return None
    

    Docs: process_request

    Also, don't forget to enable it in settings.py

    MIDDLEWARE_CLASSES = (
        ...
        'path.to.your.AuthRequiredMiddleware',
    )
    
    0 讨论(0)
  • 2021-01-30 23:55

    As of Django 1.10, the custom middleware classes must implement the new style syntax. You can use the following class to verify that the user is logged in while trying to access any views.

    from django.shortcuts import HttpResponseRedirect
    
    
    class AuthRequiredMiddleware(object):
        def __init__(self, get_response):
            self.get_response = get_response
    
        def __call__(self, request):
            # Code to be executed for each request before
            # the view (and later middleware) are called.
    
            response = self.get_response(request)
            if not request.user.is_authenticated: # in Django > 3 this is a boolean
                return HttpResponseRedirect('login')
            
            # Code to be executed for each request/response after
            # the view is called.
    
            return response
    
    0 讨论(0)
  • 2021-01-30 23:57

    There is a simpler way to do this, just add the "login_url" parameter to @login_required and if the user is not login he will be redirected to the login page. You can find it here

    from django.contrib.auth.decorators import login_required
    
    @login_required(login_url='/accounts/login/')
    def my_view(request):
        ...
    
    0 讨论(0)
  • 2021-01-30 23:57

    You can avoid specifying login_url by setting LOGIN_URL.

    In settings.py:

    LOGIN_URL = '<some_url>'
    

    In views.py:

    @login_required
    def some_view_function(request):
    

    If you need redirect within a view function, you can do so with:

    return redirect_to_login(request.get_full_path())
    
    0 讨论(0)
提交回复
热议问题