-
Notifications
You must be signed in to change notification settings - Fork 35
/
partials.py
98 lines (80 loc) · 2.83 KB
/
partials.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from django.core.paginator import Paginator
from django.http.response import HttpResponse
from django.template.response import TemplateResponse
from django.views.decorators.http import require_POST
from render_block import render_block_to_string
from ..models import Monster
from ..utils import for_htmx
def toggle_with_separate_partials(request):
return TemplateResponse(
request,
"toggle_with_separate_partials.html",
{
"monsters": Monster.objects.all(),
},
)
@require_POST
def toggle_item(request, monster_id):
monster = Monster.objects.get(id=monster_id)
monster.is_happy = not monster.is_happy
monster.save()
return TemplateResponse(request, "_toggle_item_partial.html", {"monster": monster})
def get_page_by_request(request, queryset, paginate_by=6):
return Paginator(queryset, per_page=paginate_by).get_page(request.GET.get("page"))
def paging_with_separate_partials(request):
if request.headers.get("Hx-Request", False):
template_name = "_page_and_paging_controls.html"
else:
template_name = "paging_with_separate_partials.html"
return TemplateResponse(
request,
template_name,
{
"page_obj": get_page_by_request(request, Monster.objects.all()),
},
)
@for_htmx(use_template="_page_and_paging_controls.html")
def paging_with_separate_partials_improved(request):
return TemplateResponse(
request,
"paging_with_separate_partials.html",
{
"page_obj": get_page_by_request(request, Monster.objects.all()),
},
)
def paging_with_inline_partials(request):
template_name = "paging_with_inline_partials.html"
context = {
"page_obj": get_page_by_request(request, Monster.objects.all()),
}
if request.headers.get("Hx-Request", False):
rendered_block = render_block_to_string(
template_name, "page-and-paging-controls", context=context, request=request
)
return HttpResponse(content=rendered_block)
return TemplateResponse(
request,
template_name,
context,
)
@for_htmx(use_block="page-and-paging-controls")
def paging_with_inline_partials_improved(request):
return TemplateResponse(
request,
"paging_with_inline_partials.html",
{
"page_obj": get_page_by_request(request, Monster.objects.all()),
},
)
# Similar to above, but with better Locality Of Behaviour,
# because the template specifies the "internal routing"
# of which block to use.
@for_htmx(use_block_from_params=True)
def paging_with_inline_partials_improved_lob(request):
return TemplateResponse(
request,
"paging_with_inline_partials_improved_lob.html",
{
"page_obj": get_page_by_request(request, Monster.objects.all()),
},
)