-
Notifications
You must be signed in to change notification settings - Fork 33
/
forms.py
34 lines (26 loc) · 1.1 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from django.contrib import messages
from django.forms import ModelForm
from django.forms.fields import DateField
from django.forms.widgets import DateInput
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from htmx_patterns.form_renderers import BulmaFormMixin
from ..form_utils import htmx_form_validate
from ..models import Monster
class CreateMonsterForm(BulmaFormMixin, ModelForm):
date_of_birth = DateField(initial=None, widget=DateInput(attrs={"type": "date"}))
do_htmx_validation = True
class Meta:
model = Monster
fields = ["name", "is_happy", "date_of_birth", "type"]
@htmx_form_validate(form_class=CreateMonsterForm)
def form_validation(request):
if request.method == "POST":
form = CreateMonsterForm(request.POST)
if form.is_valid():
monster = form.save()
messages.info(request, f"Monster {monster.name} created. You can make another.")
return redirect(".")
else:
form = CreateMonsterForm()
return TemplateResponse(request, "form_validation.html", {"form": form})