Skip to content

Commit

Permalink
Using the Django authentication framework - Login and Logout
Browse files Browse the repository at this point in the history
  • Loading branch information
yahyaaly151989 committed Sep 18, 2023
1 parent 0960710 commit faf2014
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 23 deletions.
Binary file modified Module04/bookmarks/account/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file modified Module04/bookmarks/account/__pycache__/views.cpython-311.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions Module04/bookmarks/account/templates/account/dashboard.html
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 %}
4 changes: 2 additions & 2 deletions Module04/bookmarks/account/templates/account/login.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html"%}
{% comment %} {% extends "base.html"%}

{% block title %}Login{% endblock %}

Expand All @@ -9,4 +9,4 @@ <h1>Login Page</h1>
{% csrf_token %}
<p><input type="submit" value="Login"></p>
</form>
{% endblock %}
{% endblock %}
25 changes: 25 additions & 0 deletions Module04/bookmarks/account/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@
<body>
<div id="header">
<span class="logo">Bookmarks</span>
{% if request.user.is_authenticated %}
<ul class='menu'>
<li {% if section == 'dashboard' %} class="selected" {% endif %}>
<a href="{% url 'dashboard' %}">My Dashboard</a>
</li>

<li {% if section == 'images' %} class="selected" {% endif %}>
<a href="#">Images</a>
</li>

<li {% if section == 'people' %} class="selected" {% endif %}>
<a href="#">People</a>
</li>
</ul>
{% endif %}

<span class="user">
{% if request.user.is_authenticated %}
Hello {{ request.user.first_name|default:request.user.username }}
<a href="{% url 'logout' %}">Logout</a>
{% else %}
<a href="{% url 'login' %}">Login</a>
{% endif %}
</span>

</div>

<div id="content">
Expand Down
11 changes: 11 additions & 0 deletions Module04/bookmarks/account/templates/registration/logged_out.html
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 Module04/bookmarks/account/templates/registration/login.html
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 %}
7 changes: 6 additions & 1 deletion Module04/bookmarks/account/urls.py
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'),
]
45 changes: 25 additions & 20 deletions Module04/bookmarks/account/views.py
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 Module04/bookmarks/bookmarks/__pycache__/settings.cpython-311.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions Module04/bookmarks/bookmarks/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,7 @@
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

LOGIN_REDIRECT_URL = 'dashboard'
LOGIN_URL = 'login'
LOGOUT_URL = 'logout'
Binary file modified Module04/bookmarks/db.sqlite3
Binary file not shown.
14 changes: 14 additions & 0 deletions Module04/bookmarks/me.txt
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

0 comments on commit faf2014

Please sign in to comment.