forked from spookylukey/django-htmx-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodals.py
More file actions
46 lines (38 loc) · 1.19 KB
/
modals.py
File metadata and controls
46 lines (38 loc) · 1.19 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
import json
from django.forms import ModelForm
from django.http import HttpRequest, HttpResponse
from django.template.response import TemplateResponse
from ..models import Monster
from ..utils import for_htmx
@for_htmx(use_block_from_params=True)
def main(request: HttpRequest):
return TemplateResponse(
request,
"modals_main.html",
{
"monsters": Monster.objects.all(),
},
)
class CreateMonsterForm(ModelForm):
class Meta:
model = Monster
fields = ["name", "is_happy"]
@for_htmx(use_block_from_params=True)
def create_monster(request: HttpRequest):
if request.method == "POST":
form = CreateMonsterForm(request.POST)
if form.is_valid():
monster = form.save()
return HttpResponse(
headers={
"Hx-Trigger": json.dumps(
{
"closeModal": True,
"monsterCreated": monster.id,
}
)
}
)
else:
form = CreateMonsterForm()
return TemplateResponse(request, "modals_create_monster.html", {"form": form})