Django Custom Login Page Use login_required Decorator

We have implemented a register/login system that uses Django’s built-in auth module in Django User Registration And Login Use Built-in Authorization Example.

But when you browse any URL other than the home page ( login page ), for example, http://127.0.0.1:8000/user/register_success/, you will find that even you do not log in, the target page content will be shown also.

This is not what we want, it is better to redirect the user to the login page when the user does not log in. This article will tell you how to do it.

1. Decorate Django View Method With login_required Decorator.

  1. Django provides a login_required decorator which can be applied to the view method.
  2. Then when the user requests that view method that has the decorator, it will check whether the user has logged in or not.
  3. If the user does not log in, it will redirect the user to the login page. To implement this, you need to follow the below steps.
  4. Import login_required class from django.contrib.auth.decorators package.
    from django.contrib.auth.decorators import login_required
  5. Add login_required decorator to the desired view method like below.
    @login_required()
    def register_success(request):
        # get user name, password, email value from session.
        user_name = request.session.get('user_name','')
        user_password = request.session.get('user_password', '')
        user_email = request.session.get('user_email', '')
        # pass user_name, user_password and user_email to display web page.
        return render(request, 'user_register_login/user_register_success.html', {'user_name':user_name,'user_password':user_password,'user_email':user_email})
    
  6. Now when you request the view method in the web browser again, http://127.0.0.1:8000/user/register_success/, it will show you a 404 page not found error page.
  7. This is because when Django finds the requested view method has login_required decorator, it will redirect the user request to http://127.0.0.1:8000/accounts/login/ page by default, but the page does not exist, but we can fix this error in the next section.

2. Set login_required Login Redirect URL.

  1. To resolve the page not found error when user request login_required decorated view method. We can use one of below three methods to fix it.
  2. Set login_url property value of the login_required decorator. But this method can only modify one view method login redirect page at one time.
  3. If you want to modify the multiple view methods login page, you should write multiple times.
    # When not logged in user request this method, then will redirect request to http://127.0.0.1:8000/user/register page.
    @login_required(login_url='/user/register')
    def register_success(request):
        # get user name, password, email value from session.
  4. Set LOGIN_URL globally in Django project settings.py file. This can avoid setting the same login_url value for each login_required decorated view method multiple times.
  5. And if you want to change the login_url page, you can edit it in the settings.py file easily. So add the below code in the settings.py file.
    LOGIN_URL = '/user'
    
    LOGIN_REDIRECT_URL='/'
  6. Since the default login_required decorator’s login page URL is http://127.0.0.1:8000/accounts/login/, so we can add a URL mapping for the request path /accounts/login/ in the project urls.py file like below.
    rlpatterns = [
        
        url(r'^user/', include('user_register_login.urls')),
        
        # map login_required default login page url to user register app home page.
    
        url(r'^accounts/login/', include('user_register_login.urls')),
    ]

1 thought on “Django Custom Login Page Use login_required Decorator”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.