Show form errors: view rewriting with methods

This commit is contained in:
Edgar P. Burkhart 2022-05-22 18:52:57 +02:00
parent 11c215e4d8
commit c3c328fdd6
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
8 changed files with 89 additions and 88 deletions

View file

@ -6,6 +6,14 @@ form {
line-height: 2rem;
}
form ul.errorlist {
grid-column: 1 / -1;
color: var(--red);
font-weight: 550;
list-style-type: "! ";
margin: 0;
}
form input,
form select,
form textarea {

View file

@ -11,7 +11,7 @@
{% block body %}
<h1><i class="fa fa-{{ category.icon }}"></i> {{ category }}</h1>
<form action="{% url 'update_category' category.id %}" method="post">
<form action="{% url 'category' category.id %}" method="post">
{% csrf_token %}
{{ form }}
<div class="buttons">

View file

@ -1,5 +1,5 @@
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
{% endfor %}
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}</label>
{{ field }}
{% endfor %}

View file

@ -29,7 +29,7 @@
{% for snap in snapshots %}
<div class="snapshot {% cycle 'w' 'g' %}">
<span class="date num center">
<a href="{% url 'snapshot' snap.date %}">{{ snap.date|date:"Y-m-d" }}</a>
<a href="{% url 'snapshot' snap.id %}">{{ snap.date|date:"Y-m-d" }}</a>
</span>
<span class="value num right">{% value snap.value %}</span>
<span class="diff num right">

View file

@ -21,7 +21,7 @@
{{ snapshot }}
</h1>
<form action="{% url 'update_snapshot' snapshot.id %}" method="post">
<form action="{% url 'snapshot' snapshot.id %}" method="post">
{% csrf_token %}
{{ form }}
<div class="buttons">

View file

@ -10,8 +10,9 @@
<h1>{{ transaction }}</h1>
{% spaceless %}
<form id="transaction" action="{% url 'update_transaction' transaction.id %}" method="post">
<form id="transaction" action="{% url 'transaction' transaction.id %}" method="post">
{% csrf_token %}
<input type="hidden" name="form" value="transaction" />
{{ form }}
<div class="buttons">
<a href="{% url 'del_transaction' transaction.id %}"><input type="button" value="Delete" class="btn del" /></a>
@ -35,8 +36,9 @@
</div>
<h3>Add Invoice</h3>
<form action="{% url 'add_invoice' transaction.id %}" method="post" enctype="multipart/form-data">
<form action="{% url 'transaction' transaction.id %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="hidden" name="form" value="invoice" />
{{ invoice_form }}
<div class="buttons"><input class="btn" type="submit" value="Add Invoice" /></div>
</form>

View file

@ -9,23 +9,13 @@ urlpatterns = [
path("transactions", views.TransactionListView.as_view(), name="transactions"),
path("transaction", views.transaction, name="transaction"),
path("transaction/<uuid>", views.transaction, name="transaction"),
path(
"transaction/<uuid>/update", views.update_transaction, name="update_transaction"
),
path("transaction/<uuid>/del", views.del_transaction, name="del_transaction"),
path("transaction/<uuid>/add_invoice", views.add_invoice, name="add_invoice"),
path(
"transaction/<uuid>/del_invoice/<invoice_id>",
views.del_invoice,
name="del_invoice",
),
path("del_invoice/<uuid>/<invoice_id>", views.del_invoice, name="del_invoice"),
path("invoice/<uuid>", views.invoice, name="invoice"),
path("category", views.category, name="category"),
path("category/<uuid>", views.category, name="category"),
path("category/<uuid>/update", views.update_category, name="update_category"),
path("category/<uuid>/del", views.del_category, name="del_category"),
path("snapshot", views.snapshot, name="snapshot"),
path("snapshot/<date>", views.snapshot, name="snapshot"),
path("snapshot/update/<uuid>", views.update_snapshot, name="update_snapshot"),
path("snapshot/<date>/del", views.del_snapshot, name="del_snapshot"),
path("snapshot/<uuid>", views.snapshot, name="snapshot"),
path("snapshot/<uuid>/del", views.del_snapshot, name="del_snapshot"),
]

View file

@ -50,32 +50,41 @@ class TransactionListView(LoginRequiredMixin, ListView):
@login_required
def transaction(request, uuid=None):
_form = None
_inv_form = None
_invoices = []
if request.method == "GET":
if uuid is None:
_transaction = Transaction()
_invoices = []
else:
_transaction = get_object_or_404(Transaction, id=uuid)
_invoices = Invoice.objects.filter(transaction=_transaction)
elif request.method == "POST":
if request.POST["form"] == "transaction":
_transaction, _ = Transaction.objects.get_or_create(id=uuid)
_form = TransactionForm(request.POST, instance=_transaction)
if _form.is_valid():
_form.save()
_inv_form = InvoiceForm(instance=Invoice(transaction=_transaction))
elif request.POST["form"] == "invoice":
_transaction = get_object_or_404(Transaction, id=uuid)
_invoice = Invoice(transaction=_transaction)
_inv_form = InvoiceForm(request.POST, request.FILES, instance=_invoice)
if _inv_form.is_valid():
_inv_form.save()
return render(
request,
"main/transaction.html",
{
"transaction": _transaction,
"form": TransactionForm(instance=_transaction),
"invoices": _invoices,
"invoice_form": InvoiceForm(instance=Invoice(transaction=_transaction)),
"form": _form or TransactionForm(instance=_transaction),
"invoices": _invoices or Invoice.objects.filter(transaction=_transaction),
"invoice_form": _inv_form
or InvoiceForm(instance=Invoice(transaction=_transaction)),
},
)
@login_required
def update_transaction(request, uuid):
_transaction, _ = Transaction.objects.get_or_create(id=uuid)
_form = TransactionForm(request.POST, instance=_transaction)
_form.save()
return redirect(transaction, uuid=uuid)
@login_required
def del_transaction(request, uuid):
_transaction = get_object_or_404(Transaction, id=uuid)
@ -90,15 +99,6 @@ def invoice(request, uuid):
return HttpResponse(_file.read(), content_type="application/pdf")
@login_required
def add_invoice(request, uuid):
_transaction = get_object_or_404(Transaction, id=uuid)
_invoice = Invoice(transaction=_transaction)
_form = InvoiceForm(request.POST, request.FILES, instance=_invoice)
_form.save()
return redirect(transaction, uuid=uuid)
@login_required
def del_invoice(request, uuid, invoice_id):
_invoice = get_object_or_404(Invoice, id=invoice_id)
@ -108,31 +108,29 @@ def del_invoice(request, uuid, invoice_id):
@login_required
def category(request, uuid=None):
if request.method == "GET":
if uuid is None:
_category = Category()
_transactions = None
else:
_category = get_object_or_404(Category, id=uuid)
_transactions = Transaction.objects.filter(category=_category)
_form = CategoryForm(instance=_category)
elif request.method == "POST":
_category, _ = Category.objects.get_or_create(id=uuid)
_form = CategoryForm(request.POST, instance=_category)
if _form.is_valid():
_form.save()
return render(
request,
"main/category.html",
{
"category": _category,
"form": CategoryForm(instance=_category),
"transactions": _transactions,
"form": _form,
"transactions": Transaction.objects.filter(category=_category),
},
)
@login_required
def update_category(request, uuid):
_category, _ = Category.objects.get_or_create(id=uuid)
_form = CategoryForm(request.POST, instance=_category)
_form.save()
return redirect(category, uuid=uuid)
@login_required
def del_category(request, uuid):
_category = get_object_or_404(Category, id=uuid)
@ -141,23 +139,17 @@ def del_category(request, uuid):
@login_required
def snapshot(request, date=None):
if date is None:
def snapshot(request, uuid=None):
if request.method == "GET":
if uuid is None:
_snapshot = Snapshot()
else:
_snapshot = get_object_or_404(Snapshot, date=date)
return render(
request,
"main/snapshot.html",
{
_snapshot = get_object_or_404(Snapshot, id=uuid)
context = {
"snapshot": _snapshot,
"form": SnapshotForm(instance=_snapshot),
},
)
@login_required
def update_snapshot(request, uuid):
}
elif request.method == "POST":
try:
_snapshot = Snapshot.objects.get(id=uuid)
except Snapshot.DoesNotExist:
@ -165,11 +157,20 @@ def update_snapshot(request, uuid):
_form = SnapshotForm(request.POST, instance=_snapshot)
if _form.is_valid():
_form.save()
return redirect(snapshot, date=_snapshot.date)
context = {
"snapshot": _snapshot,
"form": _form,
}
return render(
request,
"main/snapshot.html",
context,
)
@login_required
def del_snapshot(request, date):
_snapshot = get_object_or_404(Snapshot, date=date)
def del_snapshot(request, uuid):
_snapshot = get_object_or_404(Snapshot, id=uuid)
_snapshot.delete()
return redirect(index)