From the course: Django Essential Training

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

User authentication in two simple steps

User authentication in two simple steps

From the course: Django Essential Training

User authentication in two simple steps

- [Instructor] So far, you've learned that Django comes with a powerful authentication system already ready to be used. Now let's learn how to use it. First, let's go back to our home app and create a second template. Let's call it authorized. And in here, let's create a simple HTML that has a title, you are in a restricted area. Now we can go back to the views file and create a very similar function to home. But this time we're going to display the authorized template, def authorized request. Then it's going to return render request. Now, let's add the home/authorized.html and empty square brackets. Yeah, so now we have exactly the same thing, and here comes the wonderful Django simplicity. In here, let's import from django.contrib.auth.decorators. Let's import login_required. So now we can add this function as a decorator to our authorized function. That's it, that's all we need to do to block the access of this webpage if a user is not logged in. To finalize, let's go to the…

Contents