Skip to content

Commit a48eed1

Browse files
committed
add django authentication tutorial
1 parent c4508bc commit a48eed1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+446
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
225225
- [How to Build an English Dictionary App with Django in Python](https://www.thepythoncode.com/article/build-dictionary-app-with-django-and-pydictionary-api-python). ([code](web-programming/djangodictionary))
226226
- [How to Build a CRUD Application using Django in Python](https://www.thepythoncode.com/article/build-bookstore-app-with-django-backend-python). ([code](web-programming/bookshop-crud-app-django))
227227
- [How to Build a Weather App using Django in Python](https://www.thepythoncode.com/article/weather-app-django-openweather-api-using-python). ([code](web-programming/django-weather-app))
228+
- [How to Build an Authentication System in Django](https://www.thepythoncode.com/article/authentication-system-in-django-python). ([code](web-programming/django-authentication))
228229

229230
- ### [GUI Programming](https://www.thepythoncode.com/topic/gui-programming)
230231
- [How to Make a Text Editor using Tkinter in Python](https://www.thepythoncode.com/article/text-editor-using-tkinter-python). ([code](gui-programming/text-editor))

Diff for: web-programming/django-authentication/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Build an Authentication System in Django](https://www.thepythoncode.com/article/authentication-system-in-django-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<!doctype html>
2+
3+
<html lang="en">
4+
<head>
5+
<!-- Required meta tags -->
6+
<meta charset="utf-8">
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
9+
10+
<!-- Bootstrap CSS -->
11+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
12+
<title>{% block title %} Simple site {% endblock %}</title>
13+
</head>
14+
<body >
15+
16+
{%block body%}
17+
<header class = "header fixed-top">
18+
19+
20+
<nav class="navbar navbar-expand-lg navbar-light bg-light">
21+
<div class="container-fluid">
22+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="true" aria-label="Toggle navigation">
23+
<span class="navbar-toggler-icon"></span>
24+
</button>
25+
<div class="collapse navbar-collapse" id="navbarResponsive">
26+
{% if user.is_authenticated%}
27+
<ul class="navbar-nav ms-auto">
28+
<li class="nav-item active">
29+
30+
<li class="nav-item">
31+
<a class="nav-item nav-link dropdown-toggle mr-md-2" href="#" id="userMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{user.username}}</a>
32+
</li>
33+
<li class="nav-item">
34+
<a class="btn btn-primary ml-auto"href="{% url 'logout'%}">Log out<span class="sr-only"></span></a>
35+
36+
</li>
37+
38+
</ul>
39+
40+
</div>
41+
</div>
42+
43+
44+
45+
46+
{%else%}
47+
<button type="button" class="navbar-toggler"
48+
data-toggle="collapse" data-target="#mainMenu" aria-controls="mainMenu" aria-expanded="false" aria-haspopup="true" aria-label="Toggle navigation">
49+
<span class="navbar-toggler-icon"></span> </button>
50+
51+
<form class="form-inline ms-auto" >
52+
<a href="{% url 'login'%}" class="btn btn-secondary" color="white">Log in</a>
53+
<a href="{% url 'signup'%}" class = "btn btn-primary ml-3">Sign up</a>
54+
</form>
55+
{% endif%}
56+
57+
</nav>
58+
59+
</header>
60+
61+
<div class="container" >
62+
{% block content %}{% endblock %}
63+
</div>
64+
65+
66+
{% endblock body%}
67+
68+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
69+
</body>
70+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends 'base.html'%}
2+
3+
{% block content%}
4+
5+
<div class="container mt-5">
6+
<h3>Hello {{user}}</h3>
7+
<h2>This is a simple site </h2>
8+
</div>
9+
<a href="{% url 'logout'%}">logout</a>
10+
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
3+
{% extends 'base.html'%}
4+
{% block content%}
5+
6+
7+
8+
9+
<div class="container mt-5" style="background-color:#BCD4E6;">
10+
<section class=" d-flex align-items-center" >
11+
<div class=" mb-5 container">
12+
<div class="row">
13+
<div class="" style = "height:120px;">
14+
15+
</div>
16+
17+
<div class="mt-5 col-lg-6 d-flex flex-column justify-content-center" >
18+
19+
<h1 ><b>This is login and sign up landing page test!</b></h1>
20+
<h4 class="mt-5" >Let's try </h4>
21+
<div >
22+
<div class="mt-5 text-center text-lg-start">
23+
<a href="#" class="btn-get-started scrollto d-inline-flex align-items-center justify-content-center align-self-center">
24+
<a class="btn btn-primary btn-lg px-4 me-sm-3" href="{% url 'signup'%}" >Get Started</a>
25+
<a class="btn btn-secondary btn-lg" href="{% url 'login'%}" >Log in</a>
26+
</a>
27+
</div>
28+
</div>
29+
</div>
30+
31+
32+
</div>
33+
</div>
34+
35+
</section>
36+
37+
38+
<!-- Footer-->
39+
<footer class=" container py-2 mt-5 bg-dark">
40+
<div class="container px-5 "><p class="m-0 text-center text-white">Copyright &copy; Me 2022</p></div>
41+
</footer>
42+
</div>
43+
44+
{% endblock%}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{% extends 'base.html' %}
2+
3+
{% block body %}
4+
<div class="container">
5+
<h1 class="text-center logo my-4">
6+
<a href="{% url 'home' %}">Simple site</a>
7+
</h1>
8+
<div class="row justify-content-center">
9+
<div class="col-lg-4 col-md-6 col-sm-8">
10+
<div class="card">
11+
<div class="card-body">
12+
<h3 class="card-title">Log in</h3>
13+
<form method="post" novalidate>
14+
{% csrf_token %}
15+
{{ form.as_p }}
16+
<button type="submit" class="btn btn-primary btn-block">Log in</button>
17+
</form>
18+
</div>
19+
<div class="card-footer text-muted text-center">
20+
New to Simple site? <a href="{% url 'signup' %}">Sign up</a>
21+
</div>
22+
</div>
23+
24+
</div>
25+
</div>
26+
</div>
27+
28+
{% endblock %}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{% extends 'base.html' %}
2+
3+
{% block content %}
4+
5+
6+
7+
8+
<div class="container mt-5">
9+
<h2>Sign up</h2>
10+
<form method="post">
11+
{% csrf_token %}
12+
{% for field in form %}
13+
<p>
14+
{{ field.label_tag }}<br>
15+
{{ field }}
16+
{% if field.help_text %}
17+
<small style="color: grey">{{ field.help_text }}</small>
18+
{% endif %}
19+
{% for error in field.errors %}
20+
<p style="color: red">{{ error }}
21+
{% endfor %}
22+
</p>
23+
{% endfor %}
24+
<button type="submit" class="btn btn-primary">Create an account</button>
25+
</form>
26+
</div>
27+
28+
29+
{% endblock %}

Diff for: web-programming/django-authentication/accounts/account/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AccountConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'account'

Diff for: web-programming/django-authentication/accounts/account/migrations/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from django.shortcuts import render,redirect
2+
from django.contrib.auth import login,logout
3+
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
4+
5+
# Create your views here.
6+
def home(request):
7+
return render(request,'home.html')
8+
9+
def landing_page(request):
10+
return render(request,'landing_page.html')
11+
12+
def signup(request):
13+
if request.method == 'POST':
14+
form = UserCreationForm(request.POST)
15+
if form.is_valid():
16+
user = form.save()
17+
login(request, user)
18+
return redirect('home')
19+
else:
20+
form = UserCreationForm()
21+
return render(request, 'signup.html', {'form': form})
22+
23+
24+
25+
def log_in(request):
26+
if request.method == "POST":
27+
form = AuthenticationForm(data=request.POST)
28+
if form.is_valid():
29+
user = form.get_user()
30+
login(request,user)
31+
return redirect('home')
32+
else:
33+
form = AuthenticationForm()
34+
return render(request,'login.html', {"form":form})
35+
36+
37+
def log_out(request):
38+
logout(request)
39+
return redirect('landing_page')

Diff for: web-programming/django-authentication/accounts/accounts/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for accounts project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'accounts.settings')
15+
16+
application = get_asgi_application()

0 commit comments

Comments
 (0)