From 8f3b1f817afef079008577f023755836fb2c3b6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vili=20Sinerv=C3=A4?= Date: Sun, 24 Nov 2024 16:11:22 +0200 Subject: [PATCH] Add basic login/logout --- notesapp/settings.py | 2 +- notesapp/templates/index.html | 10 ++++++++++ notesapp/templates/login.html | 16 ++++++++++++++++ notesapp/urls.py | 6 ++++-- notesapp/views.py | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 notesapp/templates/index.html create mode 100644 notesapp/templates/login.html create mode 100644 notesapp/views.py diff --git a/notesapp/settings.py b/notesapp/settings.py index c21dac0..8b2d878 100644 --- a/notesapp/settings.py +++ b/notesapp/settings.py @@ -54,7 +54,7 @@ ROOT_URLCONF = 'notesapp.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], + 'DIRS': [ 'notesapp/templates', ], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ diff --git a/notesapp/templates/index.html b/notesapp/templates/index.html new file mode 100644 index 0000000..68b5c41 --- /dev/null +++ b/notesapp/templates/index.html @@ -0,0 +1,10 @@ + + + +
+ {% csrf_token %} + +
+

Hello, logged in user {{user.username }}!

+ + diff --git a/notesapp/templates/login.html b/notesapp/templates/login.html new file mode 100644 index 0000000..ff8187d --- /dev/null +++ b/notesapp/templates/login.html @@ -0,0 +1,16 @@ + + + + {% if login_failed %} +

Incorrect username or password!

+ {% endif %} +
+ {% csrf_token %} +
+

+
+

+ +
+ + diff --git a/notesapp/urls.py b/notesapp/urls.py index 51c4d51..5bcdbe1 100644 --- a/notesapp/urls.py +++ b/notesapp/urls.py @@ -13,9 +13,11 @@ Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ -from django.contrib import admin from django.urls import path +from notesapp import views 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'), ] diff --git a/notesapp/views.py b/notesapp/views.py new file mode 100644 index 0000000..86f17ee --- /dev/null +++ b/notesapp/views.py @@ -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")