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