Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Logging In and Out

00:00 Logging In and Out. As you saw in the last section, Django will try to use a template called registration/login.html, which currently doesn’t exist, leading to the error that you saw.

00:15 Go ahead and create it using the code seen on-screen to display a Login heading, followed by login form.

00:39 The {% csrf_token %} line inserts a cross-site request forgery (CSRF) token, which is required by every Django form to help ensure security.

00:51 Django uses a dictionary, also known as a context, to pass data to a template while rendering it. In this case, a variable called form will already be included in the context.

01:02 All you need to do is display it. Using {{ form.as_p }} will render the form as a series of HTML paragraphs. This makes it look a bit nicer than just using {{ form }}.

01:18 Finally, there’s a button for submitting the form and, at the end of the template, a link that will take your users back to the dashboard.

01:30 Head back to your browser and refresh the login page. It should now appear in a basic form with the Username and Password fields present, along with the Login button.

01:42 You can further improve the looks of the form by adding a small CSS script to the base template, as seen on-screen.

02:01 By adding the code you’ve just seen to the base template, you’ll improve the looks of all of your forms, not just the one in the dashboard. Refreshing the page once more, you should see a login page with improved appearance.

02:18 Use the credentials of your admin user and press Login, but don’t be alarmed if you see an error, as you’ll see on-screen. According to the error message, Django can’t find a path for accounts/profile/, which is the default destination for your users after a successful login.

02:37 Instead of creating a new view, it makes much more sense to reuse the dashboard view here. Fortunately, Django makes it easy to change the default redirection.

02:48 All you need to do is add one line at the end of the settings file.

02:59 Try to log in again, and this time, you should be redirected to the dashboard without any errors. Now your users can log in, but it’s probably a good idea to let them log out as well.

03:12 This process is more straightforward because there’s no form. They just need to click a link. After that, Django will redirect your users to accounts/logout/ and will try to use a template called registration/logged_out.html.

03:29 However, just like before, you can change that redirection. For example, it would make sense to redirect your users back to the dashboard. As you may already have guessed, all you need to do is add one line at the end of the settings file.

03:50 Now that both login and logout are working, it’s a good idea to add proper links to your dashboard.

04:06 If a user is logged in, then user.is_authenticated will return True, and the dashboard will show the Logout link. If a user is not logged in, then the user variable will be set to AnonymousUser, and user.is_authenticated will return False.

04:24 In that case, the Login link will be displayed. The updated dashboard should look as seen on-screen for non-authenticated users. And when you’re logged in, you should see something similar to what’s being seen on-screen now.

04:42 Congratulations, you just completed the most important part of the user management system: logging users in and out of the application. But there are a couple more steps ahead of you. So in the next section, you’ll take a look at what’s needed to allow users to manage their own passwords.

Varun Kapoor on Sept. 23, 2022

This did not work for me: LOGIN_REDIRECT_URL = “dashboard” do I have to also update the project urls file for this to work? Sorry I must have missed that part.

Varun Kapoor on Sept. 23, 2022

Sorry its ok my bad, I had two django projects and it was using the settings file of the project I was not adding this redirect url to. I deleted the project I did not need and then correctly pointed to the correct file in the file.

Darren Jones RP Team on Sept. 27, 2022

No problem, glad you’ve got it sorted. Everyone has done things like this!

Become a Member to join the conversation.