Add basic login/logout
This commit is contained in:
parent
28bbf51d21
commit
8f3b1f817a
5 changed files with 63 additions and 3 deletions
|
@ -54,7 +54,7 @@ ROOT_URLCONF = 'notesapp.urls'
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||||
'DIRS': [],
|
'DIRS': [ 'notesapp/templates', ],
|
||||||
'APP_DIRS': True,
|
'APP_DIRS': True,
|
||||||
'OPTIONS': {
|
'OPTIONS': {
|
||||||
'context_processors': [
|
'context_processors': [
|
||||||
|
|
10
notesapp/templates/index.html
Normal file
10
notesapp/templates/index.html
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<body>
|
||||||
|
<form action="/accounts/logout/" method="POST">
|
||||||
|
{% csrf_token %}
|
||||||
|
<input type="submit" value="Log out">
|
||||||
|
</form>
|
||||||
|
<h1>Hello, logged in user {{user.username }}!</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
16
notesapp/templates/login.html
Normal file
16
notesapp/templates/login.html
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<body>
|
||||||
|
{% if login_failed %}
|
||||||
|
<p> Incorrect username or password! </p>
|
||||||
|
{% endif %}
|
||||||
|
<form action="/accounts/login/" method="POST">
|
||||||
|
{% csrf_token %}
|
||||||
|
<label for="username">Username:</label><br>
|
||||||
|
<input type="text" id="username" name="username"><br><br>
|
||||||
|
<label for="password">Password:</label><br>
|
||||||
|
<input type="password" id="password" name="password"><br><br>
|
||||||
|
<input type="submit" value="Log in">
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -13,9 +13,11 @@ Including another URLconf
|
||||||
1. Import the include() function: from django.urls import include, path
|
1. Import the include() function: from django.urls import include, path
|
||||||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
|
||||||
"""
|
"""
|
||||||
from django.contrib import admin
|
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
from notesapp import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('admin/', admin.site.urls),
|
path('', views.index, name='index'),
|
||||||
|
path('accounts/login/', views.login_view, name='login'),
|
||||||
|
path('accounts/logout/', views.logout_view, name='logout'),
|
||||||
]
|
]
|
||||||
|
|
32
notesapp/views.py
Normal file
32
notesapp/views.py
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
from django.http import HttpResponse
|
||||||
|
from django.contrib.auth.decorators import login_required
|
||||||
|
from django.contrib.auth import authenticate, login, logout
|
||||||
|
from django.shortcuts import render, redirect
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
from django.db import transaction
|
||||||
|
|
||||||
|
@login_required()
|
||||||
|
def index(request):
|
||||||
|
return render(request, 'index.html')
|
||||||
|
|
||||||
|
def login_view(request):
|
||||||
|
if request.method == 'GET':
|
||||||
|
return render(request, 'login.html')
|
||||||
|
elif request.method == 'POST':
|
||||||
|
username = request.POST.get('username')
|
||||||
|
password = request.POST.get('password')
|
||||||
|
|
||||||
|
user = authenticate(username=username, password=password)
|
||||||
|
|
||||||
|
if user is not None:
|
||||||
|
login(request, user)
|
||||||
|
return redirect("index")
|
||||||
|
else:
|
||||||
|
return render(request, 'login.html', { 'login_failed' : True })
|
||||||
|
return redirect("index")
|
||||||
|
|
||||||
|
def logout_view(request):
|
||||||
|
if request.method == 'POST':
|
||||||
|
logout(request)
|
||||||
|
return redirect("index")
|
Reference in a new issue