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 @@ + + +
+ +Incorrect username or password!
+ {% endif %} + + + 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")