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" login(req, user)
},
status=401 redirect_url = getattr(req.GET, "next", settings.LOGIN_REDIRECT_URL)
) return redirect(redirect_url)
else:
login(req, user)
redirect_url = getattr(req.GET, "next", settings.LOGIN_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,189 +1,192 @@
<!-- 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");
:root { :root {
--font-family: "Ubuntu", sans-serif; --font-family: "Ubuntu", sans-serif;
--main-color: #333; --main-color: #333;
--secondary-color: #888; --secondary-color: #888;
--border-color: #ddd; --border-color: #ddd;
--background-color: #fff; --background-color: #fff;
}
body {
font-family: var(--font-family);
margin: 20px;
color: var(--main-color);
background-color: var(--background-color);
}
header {
text-align: left;
margin-bottom: 20px;
}
.parties ul {
list-style-type: none;
padding: 0;
margin: 0;
}
.parties li {
margin-top: 5px;
}
.parties h2 {
border-bottom: 1px solid var(--border-color);
padding-bottom: 8px;
font-size: 1.2rem;
}
.invoice-id {
color: var(--secondary-color);
}
#invoice {
border-collapse: collapse;
width: 100%;
}
#invoice th,
#invoice td {
padding: 12px;
text-align: left;
border-bottom: 1px solid var(--border-color);
}
.invoice-total {
margin-top: 20px;
text-align: right;
}
.parties-section {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.parties {
width: 48%;
}
#invoice tbody tr {
border-bottom: 1px solid var(--border-color);
}
.small-col {
width: 5%;
}
.medium-col {
width: 15%;
}
.big-col {
width: 25%;
}
.right-align {
text-align: right;
}
footer {
position: fixed;
bottom: 0;
width: 100%;
text-align: center;
left: 0;
right: 0;
padding: 0;
margin-top: 20px;
color: var(--main-color);
background-color: var(--background-color);
}
@media print {
@page {
size: auto;
margin: 0;
} }
body { body {
margin: 10mm 10mm; font-family: var(--font-family);
margin: 20px;
color: var(--main-color);
background-color: var(--background-color);
}
header {
text-align: left;
margin-bottom: 20px;
}
.parties ul {
list-style-type: none;
padding: 0;
margin: 0;
}
.parties li {
margin-top: 5px;
}
.parties h2 {
border-bottom: 1px solid var(--border-color);
padding-bottom: 8px;
font-size: 1.2rem;
}
.invoice-id {
color: var(--secondary-color);
}
#invoice {
border-collapse: collapse;
width: 100%;
}
#invoice th,
#invoice td {
padding: 12px;
text-align: left;
border-bottom: 1px solid var(--border-color);
}
.invoice-total {
margin-top: 20px;
text-align: right;
}
.parties-section {
display: flex;
justify-content: space-between;
margin-bottom: 20px;
}
.parties {
width: 48%;
}
#invoice tbody tr {
border-bottom: 1px solid var(--border-color);
}
.small-col {
width: 5%;
}
.medium-col {
width: 15%;
}
.big-col {
width: 25%;
}
.right-align {
text-align: right;
}
footer {
position: fixed;
bottom: 0;
width: 100%;
text-align: center;
left: 0;
right: 0;
padding: 0;
margin-top: 20px;
color: var(--main-color);
background-color: var(--background-color);
}
@media print {
@page {
size: auto;
margin: 0;
}
body {
margin: 10mm 10mm;
}
} }
}
</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>
<li>Název Vaší společnosti nebo jméno</li> <li>Název Vaší společnosti nebo jméno</li>
<li>Vaše adresa</li> <li>Vaše adresa</li>
<li>Vaše město, PSČ</li> <li>Vaše město, PSČ</li>
<p></p> <p></p>
<li>IČO: 123456789</li> <li>IČO: 123456789</li>
<li>DIČ: CZ123456789 jestli jste plátce DPH</li> <li>DIČ: CZ123456789 jestli jste plátce DPH</li>
<p></p> <p></p>
<li>Bankovní účet: 123-456789</li> <li>Bankovní účet: 123-456789</li>
</ul> </ul>
</div> </div>
<div class="parties"> <div class="parties">
<h2>Odběratel</h2> <h2>Odběratel</h2>
<ul> <ul>
<li>Název společnosti nebo jméno zákazníka</li> <li>Název společnosti nebo jméno zákazníka</li>
<li>Adresa zákazníka</li> <li>Adresa zákazníka</li>
<li>Město zákazníka, PSČ</li> <li>Město zákazníka, PSČ</li>
<p></p> <p></p>
<li>IČO zákazníka: 987654321</li> <li>IČO zákazníka: 987654321</li>
<li>DIČ zákazníka: CZ987654321 jestli je plátce DPH</li> <li>DIČ zákazníka: CZ987654321 jestli je plátce DPH</li>
<p></p> <p></p>
<li><strong>Datum vystavení:</strong> 16. prosince 2023</li> <li><strong>Datum vystavení:</strong> 16. prosince 2023</li>
<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>
<tr> <tr>
<th class="small-col">#</th> <th class="small-col">#</th>
<th class="small-col">Jednotka</th> <th class="small-col">Jednotka</th>
<th class="big-col">Popis položky</th> <th class="big-col">Popis položky</th>
<th class="medium-col right-align">Cena za MJ</th> <th class="medium-col right-align">Cena za MJ</th>
<th class="medium-col right-align">Celkem</th> <th class="medium-col right-align">Celkem</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<tr> <tr>
<td>1</td> <td>1</td>
<td>ks</td> <td>ks</td>
<td>Položka 1</td> <td>Položka 1</td>
<td class="right-align">50,00 Kč</td> <td class="right-align">50,00 Kč</td>
<td class="right-align">50,00 Kč</td> <td class="right-align">50,00 Kč</td>
</tr> </tr>
</tbody> </tbody>
</table> </table>
<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>