Skip to content

Commit 3df2664

Browse files
committed
Logging out Users and Checking Authentication Status
1 parent 793544a commit 3df2664

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/account/views.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.shortcuts import render, redirect
2-
from django.contrib.auth import login, authenticate
2+
from django.contrib.auth import login, authenticate, logout
33
from account.forms import RegistrationForm
44

55

@@ -23,5 +23,10 @@ def registration_view(request):
2323
return render(request, 'account/register.html', context)
2424

2525

26+
def logout_view(request):
27+
logout(request)
28+
return redirect('/')
29+
30+
2631

2732

src/mysite/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222

2323
from account.views import (
2424
registration_view,
25+
logout_view,
2526
)
2627

2728
urlpatterns = [
2829
path('admin/', admin.site.urls),
2930
path('', home_screen_view, name="home"),
3031
path('register/', registration_view, name="register"),
32+
path('logout/', logout_view, name="logout"),
3133
]

src/templates/base.html

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
<html lang=en>
33

44
<head>
5-
<div style="border: 2px solid #000;">
6-
<h1>This is the Header</h1>
7-
</div>
5+
{% if request.user.is_authenticated %}
6+
<p>Hello, {{request.user.username}} | <a href="{% url 'logout' %}">Logout</a></p>
7+
{% else %}
8+
<p><a href="{% url 'register' %}">Register</a> | <a href="#">Login</a></p>
9+
{% endif %}
10+
<hr>
11+
812
</head>
913

1014
<body>
@@ -16,9 +20,8 @@ <h1>This is the Header</h1>
1620
</body>
1721

1822
<footer>
19-
<div style="border: 2px solid #000;">
20-
<h2>This is the footer</h2>
21-
</div>
23+
<hr>
24+
<p>CodingWithMitch Blog Course | 2019</p>
2225
</footer>
2326

2427
</html>

0 commit comments

Comments
 (0)