Skip to content

Commit e47ced3

Browse files
authored
Add buttons for newer/older news entries within category (#1718)
1 parent da6ee35 commit e47ced3

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

news/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import timedelta
2+
from functools import partial
23

34
from django.conf import settings
45
from django.contrib import messages
@@ -149,6 +150,13 @@ def get_context_data(self, **kwargs):
149150
context["next_url"] = next_url
150151
context["next"] = get_published_or_none(self.object.get_next_by_publish_at)
151152
context["prev"] = get_published_or_none(self.object.get_previous_by_publish_at)
153+
category_kwarg = {f"{self.object.tag}__isnull": False}
154+
context["next_in_category"] = get_published_or_none(
155+
partial(self.object.get_next_by_publish_at, **category_kwarg)
156+
)
157+
context["prev_in_category"] = get_published_or_none(
158+
partial(self.object.get_previous_by_publish_at, **category_kwarg)
159+
)
152160
context["user_can_approve"] = self.object.can_approve(self.request.user)
153161
context["user_can_edit"] = self.object.can_edit(self.request.user)
154162
context["user_can_delete"] = self.object.can_delete(self.request.user)

templates/news/detail.html

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,27 @@ <h1 class="text-3xl">
9292
<div class="flex my-4 md:mx-auto md:w-3/4">
9393
<div class="w-1/2 text-left">
9494
{% if next %}
95-
<a href="{{ next.get_absolute_url }}" class="py-1 px-2 text-xs text-white rounded-md border md:text-sm border-orange bg-orange hover:bg-orange/75 dark:bg-charcoal dark:hover:bg-charcoal/75 dark:hover:text-orange">Newer Entries</a>
95+
<a href="{{ next.get_absolute_url }}" class="py-1 px-2 text-xs text-white rounded-md border md:text-sm border-orange bg-orange hover:bg-orange/75 dark:bg-charcoal dark:hover:bg-charcoal/75 dark:hover:text-orange">Newer Entry</a>
9696
{% endif %}
9797
</div>
9898
<div class="w-1/2 text-right">
9999
{% if prev %}
100-
<a href="{{ prev.get_absolute_url }}" class="py-1 px-2 text-xs text-white rounded-md border md:text-sm border-orange bg-orange hover:bg-orange/75 dark:bg-charcoal dark:hover:bg-charcoal/75 dark:hover:text-orange">Older Entries</a>
100+
<a href="{{ prev.get_absolute_url }}" class="py-1 px-2 text-xs text-white rounded-md border md:text-sm border-orange bg-orange hover:bg-orange/75 dark:bg-charcoal dark:hover:bg-charcoal/75 dark:hover:text-orange">Older Entry</a>
101+
{% endif %}
102+
</div>
103+
</div>
104+
{% endif %}
105+
106+
{% if next_in_category or prev_in_category %}
107+
<div class="flex my-4 md:mx-auto md:w-3/4">
108+
<div class="w-1/2 text-left">
109+
{% if next_in_category %}
110+
<a href="{{ next_in_category.get_absolute_url }}" class="py-1 px-2 text-xs text-white rounded-md border md:text-sm border-orange bg-orange hover:bg-orange/75 dark:bg-charcoal dark:hover:bg-charcoal/75 dark:hover:text-orange">Newer {{ entry.tag|title }} Entry</a>
111+
{% endif %}
112+
</div>
113+
<div class="w-1/2 text-right">
114+
{% if prev_in_category %}
115+
<a href="{{ prev_in_category.get_absolute_url }}" class="py-1 px-2 text-xs text-white rounded-md border md:text-sm border-orange bg-orange hover:bg-orange/75 dark:bg-charcoal dark:hover:bg-charcoal/75 dark:hover:text-orange">Older {{ entry.tag|title }} Entry</a>
101116
{% endif %}
102117
</div>
103118
</div>

0 commit comments

Comments
 (0)