#100DaysOfWeb in Python Transcripts
Chapter: Days 53-56: Django part 2 - registration and login
Lecture: Adding login and logout links to the navigation bar

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So you saw on the last video the login is not working yet, it just at that link, so let's implement it now
0:08 to get that working which means first of all it becomes a link, second of all it detects if I'm logged in and if so, it changes into a log out link.
0:18 The header HTML is in the base template, so I'm going to open my side templates based at HTML and I'm searching for login
0:30 and here's what we need to update. And we need a condition, and you know in Django that's if something else and if and we're going to have two
0:52 different list items. Okay. And this something is something useful that I want to teach you and that's every template has the request
1:05 object, and it comes with a user. And the user object has a property, is authenticated. And that tells us if the user is authenticated or logged in.
1:21 So in that case, I can show a logout link, and I need to point to the logout URL. And we can actually see when I make a mistake in my path
1:37 so for example I type wrong URL, this is fixable because debug is turned on, and here I see my login and logout
1:45 links and they have names, login and logout. And that means that in the template I can reference 'em with the URL name logout.
2:00 And this will dynamically transform an invalid URL which will lead to the log out action or view.
2:10 And we didn't write that because that's part of Django's native authentication system. And very similarly we can reference the login URL
2:21 with login and let's change that actually to a login to add quotes and one final change let's welcome back the user. Welcome back.
2:38 And we can access the user name attribute on user so request user.username, welcome back username log out link or not logged in, show the link as login
2:57 to add quotes. And again I'm not passing requests explicitly into this template because the request object is always available.
3:07 And you should recognize that as it's also passed into the views. So let's try this out, we've got the server running here.
3:24 Let's go back to the main URL. And that worked, I was logged in from the previous video so I got welcome back pibytes logout so let's try logout.
3:33 And here I messed something up with the URL. Probably why the syntax coloring was off. And now I see what I did. I forgot my percentage here.
3:49 It's always nice to set up your editor with color coding because then you get an indication that something really
3:57 is wrong because this text should have been yellow so I was definitely having a syntax error.
4:03 It's also the reason why I'm leaving this in the recording to emphasize that how you set up your tools in this case, your editor, can really help you
4:12 in developing your software. So save again. Let's go back. Refresh. Logout worked. This session was ended so request user is authenticated became false
4:31 hence I got the new login link and log in and routes me to the login link, so notice how this URL login
4:40 was dynamically transformed into the right account/login link. I can login again. And now request user is authenticated became true
4:53 and I got the corresponding text and link. So now we have a nice way for users to login and logout from the navigation bar.


Talk Python's Mastodon Michael Kennedy's Mastodon