forked from yahyaaly151989/Mastering_Django
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using the Django authentication framework - Login and Logout
- Loading branch information
1 parent
0960710
commit faf2014
Showing
13 changed files
with
121 additions
and
23 deletions.
There are no files selected for viewing
Binary file modified
BIN
+349 Bytes
(190%)
Module04/bookmarks/account/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified
BIN
-800 Bytes
(42%)
Module04/bookmarks/account/__pycache__/views.cpython-311.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %} Dashboard {% endblock %} | ||
|
||
{% block content %} | ||
<h1>Dashboard</h1> | ||
<p>Welcome to your dashboard.</p> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
Module04/bookmarks/account/templates/registration/logged_out.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %} Logged Out {% endblock %} | ||
|
||
{% block content %} | ||
<h1>Logged Out</h1> | ||
<p> | ||
You have been successfully logged out. | ||
You can <a href="{% url 'login' %}">Login again</a> | ||
</p> | ||
{% endblock %} |
26 changes: 26 additions & 0 deletions
26
Module04/bookmarks/account/templates/registration/login.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block title %} Login {% endblock %} | ||
|
||
{% block content %} | ||
<h1>Login</h1> | ||
|
||
{% if form.errors %} | ||
<p> | ||
Your username and password did not match. | ||
Please try again | ||
</p> | ||
|
||
{% else %} | ||
<p>Please enter your username and password.</p> | ||
{% endif %} | ||
|
||
<div class="login-form"> | ||
<form action="{% url 'login' %}" method="post"> | ||
{{ form.as_p }} | ||
{% csrf_token %} | ||
<input type="hidden" name="next" value="{{ next }}"> | ||
<p><input type="submit" value="Login"></p> | ||
</form> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
from django.urls import path | ||
from . import views | ||
from django.contrib.auth import views as auth_views | ||
|
||
|
||
urlpatterns = [ | ||
path('login', views.user_login, name='login'), | ||
# path('login/', views.user_login, name='login'), | ||
path('login/', auth_views.LoginView.as_view(), name='login'), | ||
path('logout/', auth_views.LogoutView.as_view(), name='logout'), | ||
path('dashboard/', views.dashboard, name='dashboard'), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
from django.shortcuts import render | ||
from django.http import HttpResponse | ||
from .forms import LoginForm | ||
from django.contrib.auth import authenticate, login | ||
# from django.http import HttpResponse | ||
# from .forms import LoginForm | ||
# from django.contrib.auth import authenticate, login | ||
from django.contrib.auth.decorators import login_required | ||
|
||
def user_login(request): | ||
if request.method == 'POST': | ||
form = LoginForm(request.POST) | ||
if form.is_valid(): | ||
cd = form.cleaned_data | ||
user = authenticate(request, username=cd['username'], password=cd['password']) | ||
if user is not None: | ||
if user.is_active: | ||
login(request, user) | ||
return HttpResponse("Authenticated successfully.") | ||
else: | ||
return HttpResponse("Disabled account.") | ||
else: | ||
return HttpResponse("Invalid login.") | ||
else: | ||
form = LoginForm() | ||
return render(request, 'account/login.html', {'form': form}) | ||
# def user_login(request): | ||
# if request.method == 'POST': | ||
# form = LoginForm(request.POST) | ||
# if form.is_valid(): | ||
# cd = form.cleaned_data | ||
# user = authenticate(request, username=cd['username'], password=cd['password']) | ||
# if user is not None: | ||
# if user.is_active: | ||
# login(request, user) | ||
# return HttpResponse("Authenticated successfully.") | ||
# else: | ||
# return HttpResponse("Disabled account.") | ||
# else: | ||
# return HttpResponse("Invalid login.") | ||
# else: | ||
# form = LoginForm() | ||
# return render(request, 'account/login.html', {'form': form}) | ||
|
||
@login_required | ||
def dashboard(request): | ||
return render(request, 'account/dashboard.html', {'section': 'dashboard'}) |
Binary file modified
BIN
+95 Bytes
(100%)
Module04/bookmarks/bookmarks/__pycache__/settings.cpython-311.pyc
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
• LoginView: Handles a login form and logs in a user | ||
• LogoutView: Logs out a user | ||
|
||
• PasswordChangeView: Handles a form to change the user’s password | ||
• PasswordChangeDoneView: The success view that the user is redirected to after a successful | ||
password change | ||
|
||
• PasswordResetView: Allows users to reset their password. It generates a one-time-use link | ||
with a token and sends it to a user’s email account | ||
• PasswordResetDoneView: Tells users that an email—including a link to reset their password— | ||
has been sent to them | ||
• PasswordResetConfirmView: Allows users to set a new password | ||
• PasswordResetCompleteView: The success view that the user is redirected to after successfully | ||
resetting their password |