Skip to content

Commit 6893446

Browse files
author
azmanabdlh
committed
basic auth django - (using django contrib auth)
1 parent 840351d commit 6893446

18 files changed

+108
-13
lines changed
0 Bytes
Binary file not shown.
127 Bytes
Binary file not shown.
747 Bytes
Binary file not shown.
639 Bytes
Binary file not shown.

account/admin.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
from django.contrib import admin
2-
3-
# Register your models here.

account/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from django.db import models
2-
3-
# Create your models here.
2+
from django.contrib.auth.models import AbstractUser
3+
from django.utils.translation import gettext_lazy as _

account/templates/home.html

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
<h1>Home page</h1>
1+
{% extends 'registration/_base.html' %}
2+
3+
{% block title %} Home page {% endblock %}
4+
5+
{% block content %}
6+
<h1>Home page</h1>
7+
<h3>Hai {{ user.username | capfirst }} {{ user.email }}</h3>
8+
<a href="{% url 'logout' %}">Logout</a>
9+
{% endblock %}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>{% block title %} {% endblock %}</title>
7+
<style>
8+
.errorlist {
9+
color: red;
10+
}
11+
</style>
12+
</head>
13+
<body>
14+
{% block content %}
15+
16+
{% endblock %}
17+
</body>
18+
</html>
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
<h1>Login page</h1>
1+
{% extends 'registration/_base.html' %}
2+
3+
{% block title %} Login {% endblock %}
4+
5+
{% block content %}
6+
<h1>Form Login page</h1>
7+
8+
<form method="post">
9+
{% csrf_token %}
10+
{{ form.as_ul }}
11+
12+
<button>Login</button>
13+
<a href="{% url 'register' %}">Register</a>
14+
</form>
15+
16+
{% endblock %}
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
<h1>Register page</h1>
1+
{% extends 'registration/_base.html' %}
2+
3+
{% block title %} Login {% endblock %}
4+
5+
{% block content %}
6+
<h1>Form Register page</h1>
7+
8+
<form method="post">
9+
{% csrf_token %}
10+
{{ form.as_ul }}
11+
12+
<button>Register</button>
13+
<a href="{% url 'register' %}">Login</a>
14+
</form>
15+
16+
{% endblock %}

account/urls.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,20 @@
11
from django.urls import path
2+
from django.contrib.auth.views import LoginView, LogoutView
3+
from django.views.generic import TemplateView
4+
from django.contrib.auth.decorators import login_required
5+
from django.urls import reverse_lazy
6+
7+
from .views import UserRegistrationView
8+
9+
10+
11+
urlpatterns = [
12+
path('register/', UserRegistrationView.as_view(), name='register'),
13+
path('login/', LoginView.as_view(redirect_authenticated_user=True), name='login'),
14+
path('logout/', LogoutView.as_view(), name='logout'),
15+
16+
path('home/', login_required(
17+
TemplateView.as_view(template_name = 'home.html'),
18+
login_url=reverse_lazy('login'))
19+
)
20+
]

account/views.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
from django.shortcuts import render
2+
from django.views.generic import CreateView
3+
from django.urls import reverse_lazy
4+
from django.contrib.auth.forms import UserCreationForm
25

3-
# Create your views here.
6+
7+
class UserRegistrationView(CreateView):
8+
template_name = 'registration/register.html'
9+
success_url = reverse_lazy('login')
10+
form_class = UserCreationForm
81 Bytes
Binary file not shown.

myapp/__pycache__/urls.cpython-38.pyc

161 Bytes
Binary file not shown.

myapp/settings.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
TEMPLATES = [
5959
{
6060
'BACKEND': 'django.template.backends.django.DjangoTemplates',
61-
'DIRS': [],
61+
'DIRS': ['templates'],
6262
'APP_DIRS': True,
6363
'OPTIONS': {
6464
'context_processors': [
@@ -130,3 +130,12 @@
130130
# https://docs.djangoproject.com/en/3.1/howto/static-files/
131131

132132
STATIC_URL = '/static/'
133+
134+
135+
# Auth
136+
137+
# AUTH_USER_MODEL = 'account.User'
138+
139+
LOGIN_REDIRECT_URL = '/home'
140+
141+
LOGOUT_REDIRECT_URL = '/login'

myapp/urls.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from django.contrib import admin
2-
from django.urls import path
2+
from django.urls import path, include
3+
from django.views.generic import TemplateView
34

45
urlpatterns = [
56
path('admin/', admin.site.urls),
7+
path('', TemplateView.as_view(template_name = 'index.html')),
8+
9+
# module apps
10+
path('', include('account.urls'))
611
]

templates/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{% extends 'layouts/app.html' %}
22

3-
{% block content }
3+
{% block content %}
44
<h1>Welcome page</h1>
55
<ul>
6-
<li><a href="">Register</a></li>
7-
<li><a href="">Login</a></li>
6+
<li><a href="{% url 'register' %}">Register</a></li>
7+
<li><a href="{% url 'login' %}">Login</a></li>
88
</ul>
99
{% endblock %}

templates/layouts/app.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</head>
88
<body>
99
{% block content %}
10+
1011
{% endblock %}
1112
</body>
1213
</html>

0 commit comments

Comments
 (0)