How to Create a Page that Requires a User Be Logged in with Django 


               


How to Create a Page that Requires a User Be Logged in with Django



Python


In this article, we go over how to create a page that requires a user be logged in with Django.

So the best way to do this is use the login_required decorator in Django.

To use the login_required decorator in Django, you first have to import it using the following statement, from django.contrib.auth.decorators import login_required

Then before the function-based view you want to use the login_required decorator for, you place in the statement, @login_required

This makes that function also show its view that it renders if a user is logged in. In other words, a user must be logged in, in order for this view to be shown.

So let's go over some sample code of how to use the login_required decorator in Django.

This code is present in the views.py file.



So, in the above code we import login_required from django.contrib.auth.decorators

We then have our usual HttpResponse imported from django.http

We then have our function showview.

To make a login required in order to see this view, we put in the @login_required() decorator before the function. This will only execute the function (and show the view) if a user is logged in and authenticated.

If a user isn't logged in, the URL will change by default to, http://127.0.0.1:8000/accounts/login/?next=/Blog/view3

The part that appear after the ?next= is whatever app you're in and whatever function you are running.

The main part of the URL is, http://127.0.0.1:8000/accounts/login/

By default, the @login_required decorator will route the URL to, http://127.0.0.1:8000/accounts/login/

However, you can change this.

There are 2 ways to change this default

The first way is to put the URL path that you want the @login_required() decorator to go to as a parameter.

So, if you want the URL to change to, http://127.0.0.1:8000/login/, you would specify this as a parameter to the @login_required decorator.

This is shown in the code below.



So, now if a user tries to go to that page which the showview() function renders, s/he will be rerouted to the URL, http://127.0.0.1:8000/login/

As we said, there is another way of doing this.

You can also do this same exact thing by going into the settings.py file and specifying the following line shown below.



What this line does is it specifies the default URL path that a user should be redirected to if trying to access a page that requires a user login in. It represents the login page that you should create for your site, so that a user can log in and then see the page that only logged in users can view.

Once you do this, then all you have to do is have the following code.



Now the user will be redirected to the URL, http://127.0.0.1:8000/login/

So Django's built in, @login_required decorator is a great tool for sites that have users and requires authentication in order to see certain pages.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...