Skip to content

Commit 7b93b1f

Browse files
committed
feat: re-organize context for templates using a mixin
context that will be shared across views of the districts app is moved into the DistrictContextMixin. This reduces context duplication, both when different views need the same context and also when the view may not need the context, but the template it extends does need it. Note that because of how the MRO and mixins work, DistrictContextMixin inherits from TemplateView.
1 parent 46f0969 commit 7b93b1f

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

pems/districts/templates/districts/district.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% extends "districts/index.html" %}
22

33
{% block headline %}
4-
District {{ url_district }} - {{ district_query.name }}
4+
District {{ district_number }} - {{ district.name }}
55
{% endblock headline %}
66

77
{% block district-content %}
@@ -15,7 +15,7 @@ <h2>Chart</h2>
1515
</div>
1616
<div class="row">
1717
<div class="col-lg-4 border">
18-
<h2>Details for {{ district_query.name }}</h2>
18+
<h2>Details for {{ district.name }}</h2>
1919
</div>
2020
<div class="col-lg-8 border">
2121
<h2>Map</h2>

pems/districts/templates/districts/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<!-- Side navigation -->
1212
<nav aria-labelledby="navigation-list" class="side-navigation">
1313
<ul class="list-navigation">
14-
{% for district in district_queryset %}
14+
{% for district in districts.all %}
1515
<li>
1616
<a href="{% url 'districts:district' district.number %}">District {{ district.number }}</a>
1717
</li>

pems/districts/views.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@
33
from .models import District
44

55

6-
class IndexView(TemplateView):
7-
template_name = "districts/index.html"
6+
class DistrictContextMixin:
87

98
def get_context_data(self, **kwargs):
109
context = super().get_context_data(**kwargs)
11-
context["district_queryset"] = District.objects.all()
10+
districts_ctx = {}
11+
districts_ctx["all"] = District.objects.all()
12+
context["districts"] = districts_ctx
1213
return context
1314

1415

15-
class DistrictView(TemplateView):
16+
class IndexView(DistrictContextMixin, TemplateView):
17+
template_name = "districts/index.html"
18+
19+
20+
class DistrictView(DistrictContextMixin, TemplateView):
1621
template_name = "districts/district.html"
1722

1823
def get_context_data(self, **kwargs):
1924
context = super().get_context_data(**kwargs)
20-
district = self.kwargs.get("district")
21-
context["url_district"] = district
22-
context["district_queryset"] = District.objects.all()
23-
context["district_query"] = District.objects.all().get(number=district)
25+
district_number = self.kwargs.get("district")
26+
context["district_number"] = district_number
27+
context["district"] = context.get("districts").get("all").get(number=district_number)
2428
return context

tests/pytest/pems/districts/test_views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pems.districts.models import District
55

66

7-
class TestIndexViewView:
7+
class TestIndexView:
88
@pytest.fixture
99
def view(app_request):
1010
v = views.IndexView()
@@ -17,7 +17,7 @@ def test_get_context_data(self, view):
1717

1818
context = view.get_context_data()
1919

20-
assert set(context["district_queryset"]) == set(District.objects.all())
20+
assert set(context.get("districts").get("all")) == set(District.objects.all())
2121

2222
def test_template_name(self, view):
2323
assert view.template_name == "districts/index.html"
@@ -37,7 +37,7 @@ def test_get_context_data(self, view):
3737

3838
context = view.get_context_data()
3939

40-
assert context["url_district"] == 1
40+
assert context["district_number"] == 1
4141

4242
def test_template_name(self, view):
4343
assert view.template_name == "districts/district.html"

0 commit comments

Comments
 (0)