Skip to content

Commit 34e62e4

Browse files
More types in example
1 parent 005a9ac commit 34e62e4

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

shop_list/templates/shop_list/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h1>Shop lists</h1>
2020
<a href="{% url 'shop_list:view_shop_list' shop_list.id %}">
2121
{{ shop_list.name }} ({{shop_list.created_at}})
2222
</a>
23-
{% async_include "shop_list/partials/item_list.html" shop_list=shop_list items=shop_list.item_set.all %}
23+
{% async_include "shop_list/partials/item_list.html" shop_list=shop_list items=shop_list.item_set.all food_group_percentages=food_group_percentages item_list_title=item_list_title request_time=request_time %}
2424
</li>
2525
{% endfor %}
2626
</ul>
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
<div>
2-
<h2>Product list</h2>
3-
<ul>
4-
{% for item in items %}
5-
<li>
6-
{{ item.name }}
7-
</li>
2+
<h2>{{ item_list_title }}</h2>
3+
<div>
4+
<em>Requested at: {{ request_time }}</em>
5+
</div>
6+
{{ non_passed_variable }}
7+
<div>
8+
Remember the percentages (number of food groups: {{food_group_percentages|length}}):
9+
<ul>
10+
{% for group, perc in food_group_percentages.items %}
11+
<li>
12+
{{ group }} {{perc}} %
13+
</li>
814
{% endfor %}
9-
</ul>
15+
</ul>
16+
</div>
17+
<div>
18+
<ul>
19+
{% for item in items %}
20+
<li>
21+
{{ item.name }}
22+
</li>
23+
{% endfor %}
24+
</ul>
25+
</div>
1026
</div>

shop_list/views.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from django.shortcuts import render, get_object_or_404
22
from django.http import HttpRequest, HttpResponse
33
from django.db.models.query import QuerySet
4+
from django.utils import timezone
5+
46
from shop_list.models import ShopList
57

68
DEFAULT_USE_WS = False
@@ -12,7 +14,17 @@ def index(request: HttpRequest) -> HttpResponse:
1214
use_ws = bool(int(request.GET.get('use_ws', DEFAULT_USE_WS)))
1315
shop_list_size = int(request.GET.get('size', DEFAULT_SHOP_LIST_SIZE))
1416
shop_lists: QuerySet[ShopList] = ShopList.objects.all().order_by('-created_at')[:shop_list_size]
15-
replacements = {'shop_lists': shop_lists, 'sizes': SHOP_LIST_SIZES}
17+
replacements = {
18+
'shop_lists': shop_lists,
19+
'sizes': SHOP_LIST_SIZES,
20+
'food_group_percentages': {
21+
'Carbohydrates': 40,
22+
'Proteins': 35,
23+
'Fats': 25,
24+
},
25+
'item_list_title': 'Product list',
26+
'request_time': timezone.now()
27+
}
1628
if use_ws:
1729
template_path = 'shop_list/index_with_ws.html'
1830
else:
@@ -22,5 +34,8 @@ def index(request: HttpRequest) -> HttpResponse:
2234

2335
def view_shop_list(request: HttpRequest, shop_list_id: int) -> HttpResponse:
2436
shop_list = get_object_or_404(ShopList, id=shop_list_id)
25-
replacements = {'shop_list': shop_list, 'items': shop_list.item_set.all}
37+
replacements = {
38+
'shop_list': shop_list,
39+
'items': shop_list.item_set.all
40+
}
2641
return render(request, 'shop_list/view_shop_list.html', replacements)

0 commit comments

Comments
 (0)