Django Custom Login Page Use login_required Decorator

We have implemented a register / login system use Django 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 login, the target page content will be shown also like below picture. This is not what we want, it is better to redirect user to the login page when user do not login. This article will tell you how to do it.

no login required decorated django view method returnned web page

1. Decorate Django View Method With login_required Decorator.

Django provide a login_required decorator which can be applied on view method. Then when user request that view method which has the decorator, it will check whether user has logged in or not. If not, it will redirect user to the login page. To implement this, you need to follow below steps.

  1. Import login_required class from django.contrib.auth.decorators package.
    from django.contrib.auth.decorators import login_required
  2. 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})
    
  3. Now when you request the view method in web browser again, http://127.0.0.1:8000/user/register_success/, it will show you a 404 page not found error page like below. This is because when Django find 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 do not exist, but we can fix this error in next section.
    404 page not found error when django view method has login required decorator
READ :   What Is The Difference Between R & U Flag At Python String Beginning

2. Set login_required Login Redirect URL.

To resolve page not found error when user request login_required decorated view method. We can use one of below three method to fix it.

  1. Set login_url property value of login_required decorator. But this method can only modify one view method login redirect page at one time, if you want to modify multiple view method 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.
  2. Set LOGIN_URL globally in Django project settings.py file. This can avoid set same login_url value for each login_required decorated view method multiple times. And if you want to change the login_url page, you can edit it in one settings.py file easily.
    pycharm django project files list
    Add below code in settings.py file.

    LOGIN_URL = '/user'
    
    LOGIN_REDIRECT_URL='/'
  3. 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 request path /accounts/login/ in 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 Comment

Leave a Reply

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.