1
0
Fork 0

Reorder files

This commit is contained in:
Vili Sinervä 2024-11-24 16:45:26 +02:00
parent 8f3b1f817a
commit d9a41f82bb
No known key found for this signature in database
GPG key ID: DF8FEAF54EFAC996
16 changed files with 59 additions and 13 deletions

0
notes/__init__.py Normal file
View file

3
notes/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
notes/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class NotesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'notes'

View file

3
notes/models.py Normal file
View file

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

View 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>

View 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>

3
notes/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

24
notes/urls.py Normal file
View file

@ -0,0 +1,24 @@
"""notes URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
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.urls import path
from notes import views
urlpatterns = [
path('', views.index, name='index'),
path('accounts/login/', views.login_view, name='login'),
path('accounts/logout/', views.logout_view, name='logout'),
]

41
notes/views.py Normal file
View file

@ -0,0 +1,41 @@
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, connection
# from notes.models import Note
@login_required()
def index(request):
user = request.user
# notes = Note.objects.filter(owner=user)
# notes_list = [ { 'time' : note.time, 'body' : note.body } for note in notes ]
# notes_list.sort(key=lambda note: note['time'])
# return render(request, 'index.html', { 'notes' : notes_list})
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")