Trying to add the django fucking forms

This commit is contained in:
Jakub Kropáček 2023-12-18 23:05:49 +01:00
parent 4f22f97952
commit fe0ae92c6e
7 changed files with 327 additions and 167 deletions

View file

@ -3,6 +3,4 @@ from django.contrib.auth.admin import UserAdmin
from .models import User from .models import User
# Register your models here.
admin.site.register(User, UserAdmin) admin.site.register(User, UserAdmin)

13
account/forms.py Normal file
View file

@ -0,0 +1,13 @@
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
# from .models import User
class LoginForm(AuthenticationForm):
pass
#
# class RegisterForm(UserCreationForm):
# class Meta(UserCreationForm.Meta):
# model = User
# fields = UserCreationForm.Meta.fields

View file

@ -0,0 +1,131 @@
# Generated by Django 5.0 on 2023-12-18 21:42
import django.contrib.auth.models
import django.contrib.auth.validators
import django.utils.timezone
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
("auth", "0012_alter_user_first_name_max_length"),
]
operations = [
migrations.CreateModel(
name="User",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("password", models.CharField(max_length=128, verbose_name="password")),
(
"last_login",
models.DateTimeField(
blank=True, null=True, verbose_name="last login"
),
),
(
"is_superuser",
models.BooleanField(
default=False,
help_text="Designates that this user has all permissions without explicitly assigning them.",
verbose_name="superuser status",
),
),
(
"username",
models.CharField(
error_messages={
"unique": "A user with that username already exists."
},
help_text="Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
max_length=150,
unique=True,
validators=[
django.contrib.auth.validators.UnicodeUsernameValidator()
],
verbose_name="username",
),
),
(
"first_name",
models.CharField(
blank=True, max_length=150, verbose_name="first name"
),
),
(
"last_name",
models.CharField(
blank=True, max_length=150, verbose_name="last name"
),
),
(
"email",
models.EmailField(
blank=True, max_length=254, verbose_name="email address"
),
),
(
"is_staff",
models.BooleanField(
default=False,
help_text="Designates whether the user can log into this admin site.",
verbose_name="staff status",
),
),
(
"is_active",
models.BooleanField(
default=True,
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
verbose_name="active",
),
),
(
"date_joined",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="date joined"
),
),
(
"groups",
models.ManyToManyField(
blank=True,
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
related_name="user_set",
related_query_name="user",
to="auth.group",
verbose_name="groups",
),
),
(
"user_permissions",
models.ManyToManyField(
blank=True,
help_text="Specific permissions for this user.",
related_name="user_set",
related_query_name="user",
to="auth.permission",
verbose_name="user permissions",
),
),
],
options={
"verbose_name": "user",
"verbose_name_plural": "users",
"abstract": False,
},
managers=[
("objects", django.contrib.auth.models.UserManager()),
],
),
]

View file

View file

@ -16,5 +16,17 @@
</div> </div>
<button type="submit" class="btn btn-primary">Login</button> <button type="submit" class="btn btn-primary">Login</button>
</form> </form>
{# <form class="col-md-6 offset-md-3" action="{% url 'login' %}" method="post">#}
{# {% csrf_token %}#}
{# <div class="mb-3">#}
{# <label for="username" class="form-label">Username</label>#}
{# <input type="text" class="form-control" name="username" id="username" required>#}
{# </div>#}
{# <div class="mb-3">#}
{# <label for="password" class="form-label">Password</label>#}
{# <input type="password" class="form-control" name="password" id="password" required>#}
{# </div>#}
{# <button type="submit" class="btn btn-primary">Login</button>#}
{# </form>#}
</div> </div>
{% endblock %} {% endblock %}

View file

@ -1,28 +1,30 @@
from django.conf import settings from django.conf import settings
from django.contrib.auth import authenticate, login, logout from django.contrib.auth import login, logout
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpRequest, HttpResponse, JsonResponse from django.http import HttpRequest, HttpResponse, JsonResponse
from django.shortcuts import redirect, render from django.shortcuts import redirect, render
from . import forms
def auth_login(req: HttpRequest) -> HttpResponse: def auth_login(req: HttpRequest) -> HttpResponse:
if req.method == "POST": if req.method == "POST":
username = req.POST["username"] form = forms.LoginForm(req, req.POST)
password = req.POST["password"]
user = authenticate(req, username=username, password=password) if not form.is_valid():
if not user: return render(req, "account/login.html", dict(form=form))
return JsonResponse(
{ user = form.get_user()
"message": "Invalid cretendials"
},
status=401
)
else:
login(req, user) login(req, user)
redirect_url = getattr(req.GET, "next", settings.LOGIN_REDIRECT_URL) redirect_url = getattr(req.GET, "next", settings.LOGIN_REDIRECT_URL)
return redirect(redirect_url) return redirect(redirect_url)
elif req.method == "GET": elif req.method == "GET":
return render(req, "account/login.html")
form = forms.LoginForm(req)
return render(req, "account/login.html", dict(form=form))
return HttpResponse(status=405) return HttpResponse(status=405)
@ -35,6 +37,7 @@ def auth_logout(req: HttpRequest) -> HttpResponse:
def auth_register(req: HttpRequest) -> HttpResponse: def auth_register(req: HttpRequest) -> HttpResponse:
return render(req, "account/register.html") return render(req, "account/register.html")
@login_required @login_required
def me(req: HttpRequest) -> HttpResponse: def me(req: HttpRequest) -> HttpResponse:
return render(req, "account/me.html") return render(req, "account/me.html")

View file

@ -1,9 +1,9 @@
<!-- Vaše faktura s aktualizacemi --> <!-- Vaše faktura s aktualizacemi -->
<!DOCTYPE html> <!DOCTYPE html>
<html lang="cs"> <html lang="cs">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Faktura</title> <title>Faktura</title>
<style> <style>
@import url("https://fonts.googleapis.com/css2?family=Ubuntu&display=swap"); @import url("https://fonts.googleapis.com/css2?family=Ubuntu&display=swap");
@ -82,12 +82,15 @@
.small-col { .small-col {
width: 5%; width: 5%;
} }
.medium-col { .medium-col {
width: 15%; width: 15%;
} }
.big-col { .big-col {
width: 25%; width: 25%;
} }
.right-align { .right-align {
text-align: right; text-align: right;
} }
@ -116,13 +119,13 @@
} }
} }
</style> </style>
</head> </head>
<body> <body>
<header> <header>
<h1>Faktura <span class="invoice-id">2023-0001</span></h1> <h1>Faktura <span class="invoice-id">2023-0001</span></h1>
</header> </header>
<section class="parties-section"> <section class="parties-section">
<div class="parties"> <div class="parties">
<h2>Dodavatel</h2> <h2>Dodavatel</h2>
<ul> <ul>
@ -151,9 +154,9 @@
<li><strong>Datum splatnosti:</strong> 16. prosince 2023</li> <li><strong>Datum splatnosti:</strong> 16. prosince 2023</li>
</ul> </ul>
</div> </div>
</section> </section>
<section> <section>
<h2>Detaily faktury</h2> <h2>Detaily faktury</h2>
<table id="invoice"> <table id="invoice">
<thead> <thead>
@ -179,11 +182,11 @@
<div class="invoice-total"> <div class="invoice-total">
<p><strong>Celkem: 100,00 Kč</strong></p> <p><strong>Celkem: 100,00 Kč</strong></p>
</div> </div>
</section> </section>
<footer> <footer>
<p>Fyzická osoba zapsaná v živnostenském rejstříku.</p> <p>Fyzická osoba zapsaná v živnostenském rejstříku.</p>
<p>Fakturu vygenerovala aplikace Facturio</p> <p>Fakturu vygenerovala aplikace Facturio</p>
</footer> </footer>
</body> </body>
</html> </html>