Compare commits
2 Commits
178377cfb6
...
validate-t
| Author | SHA1 | Date | |
|---|---|---|---|
| fc6b3a7fa0 | |||
| 2fc2d07a7d |
Binary file not shown.
@ -0,0 +1,46 @@
|
||||
# Generated by Django 5.0.4 on 2024-05-16 05:33
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
("main", "0003_person"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name="person",
|
||||
name="address",
|
||||
field=models.CharField(
|
||||
max_length=100,
|
||||
validators=[
|
||||
django.core.validators.MinLengthValidator(2),
|
||||
django.core.validators.MaxLengthValidator(100),
|
||||
],
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="person",
|
||||
name="city",
|
||||
field=models.CharField(
|
||||
max_length=100,
|
||||
validators=[
|
||||
django.core.validators.MinLengthValidator(2),
|
||||
django.core.validators.MaxLengthValidator(100),
|
||||
],
|
||||
),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name="person",
|
||||
name="name",
|
||||
field=models.CharField(
|
||||
max_length=100,
|
||||
validators=[
|
||||
django.core.validators.MinLengthValidator(2),
|
||||
django.core.validators.MaxLengthValidator(100),
|
||||
],
|
||||
),
|
||||
),
|
||||
]
|
||||
@ -1,10 +1,26 @@
|
||||
from django.core.validators import MaxLengthValidator, MinLengthValidator
|
||||
from django.db import models
|
||||
|
||||
|
||||
class Person(models.Model):
|
||||
name = models.CharField(max_length=100)
|
||||
address = models.CharField(max_length=100)
|
||||
city = models.CharField(max_length=100)
|
||||
name = models.CharField(
|
||||
max_length=100,
|
||||
blank=False,
|
||||
null=False,
|
||||
validators=[MinLengthValidator(2), MaxLengthValidator(100)],
|
||||
)
|
||||
address = models.CharField(
|
||||
max_length=100,
|
||||
blank=False,
|
||||
null=False,
|
||||
validators=[MinLengthValidator(2), MaxLengthValidator(100)],
|
||||
)
|
||||
city = models.CharField(
|
||||
max_length=100,
|
||||
blank=False,
|
||||
null=False,
|
||||
validators=[MinLengthValidator(2), MaxLengthValidator(100)],
|
||||
)
|
||||
|
||||
class Meta:
|
||||
db_table = "persons"
|
||||
|
||||
@ -1,14 +1,24 @@
|
||||
{% macro inline_table_row(person, is_editing) %}
|
||||
{% macro inline_table_row(person, is_editing, errors={}) %}
|
||||
{% macro render_input(field_name, value) %}
|
||||
{% set has_error = field_name in errors %}
|
||||
<input
|
||||
class="form-control {% if has_error %}is-invalid{% endif %}"
|
||||
name="{{ field_name }}"
|
||||
value="{{ value }}"
|
||||
{% if has_error %}title="{{ errors[field_name] }}"{% endif %}
|
||||
>
|
||||
{% endmacro %}
|
||||
|
||||
{% if is_editing %}
|
||||
<tr id="person-row-{{ person.pk }}" hx-target="this" hx-swap="outerHTML">
|
||||
<td>
|
||||
<input class="form-control" name="name" value="{{ person.name }}">
|
||||
{{ render_input(field_name="name", value=person.name) }}
|
||||
</td>
|
||||
<td>
|
||||
<input class="form-control" name="address" value="{{ person.address }}">
|
||||
{{ render_input(field_name="address", value=person.address) }}
|
||||
</td>
|
||||
<td>
|
||||
<input class="form-control" name="city" value="{{ person.city }}">
|
||||
{{ render_input(field_name="city", value=person.city) }}
|
||||
</td>
|
||||
<td>
|
||||
<button
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
{% from "main/components/inline_table_row.html" import inline_table_row %}
|
||||
|
||||
{{ inline_table_row(person, is_editing=is_editing) }}
|
||||
{{ inline_table_row(person, is_editing=is_editing, errors=errors) }}
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
from typing import Any
|
||||
from typing import Any, Optional
|
||||
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.handlers.wsgi import WSGIRequest
|
||||
from django.http import Http404, HttpRequest, HttpResponse
|
||||
from django.http import Http404, HttpResponse
|
||||
from django.shortcuts import render
|
||||
|
||||
from project.main.models import Person
|
||||
@ -51,16 +52,27 @@ class TableInlineEditRowView(DemoViewBase):
|
||||
return context_data
|
||||
|
||||
def post(self, request: WSGIRequest, *args, **kwargs) -> HttpResponse:
|
||||
errors: Optional[dict[str, str]] = None
|
||||
|
||||
person = get_person(pk=kwargs.get("pk"))
|
||||
person.name = request.POST.get("name")
|
||||
person.address = request.POST.get("address")
|
||||
person.city = request.POST.get("city")
|
||||
person.save()
|
||||
|
||||
try:
|
||||
person.clean_fields()
|
||||
except ValidationError as ex:
|
||||
errors = {key: value[0].message for key, value in ex.error_dict.items()}
|
||||
else:
|
||||
person.save()
|
||||
|
||||
print(errors)
|
||||
|
||||
return render(
|
||||
context={
|
||||
"person": person,
|
||||
"is_editing": False,
|
||||
"errors": errors,
|
||||
"is_editing": errors is not None,
|
||||
},
|
||||
template_name=self.template_name,
|
||||
request=request,
|
||||
|
||||
Reference in New Issue
Block a user