Add redirect to page 1 from bare URL

Previously, you could browse /board/ and /board/page/1/ in the browser
and they would be the same thing. Now, /board/ redirects to
/board/page/1/ to keep things unambiguous.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2022-05-07 16:31:32 -07:00
parent 439035f1d8
commit bd6a86169d
2 changed files with 17 additions and 4 deletions

View File

@@ -66,19 +66,23 @@
{# Page footer #}
<div class="row pagination">
{% if current_page > 1 %}
<a href="/{{board.url}}/page/{{ current_page|add:"-1" }}">{% localize on %}Prev{% endlocalize %}</a>
{% with prev_page=current_page|add:"-1" %}
<a href="{% url 'board:board_detail' board.url prev_page %}">{% localize on %}Prev{% endlocalize %}</a>
{% endwith %}
{% endif %}
[
{% for page in pages %}
{% if current_page == page %}
<strong>{{page}}</strong>
{% else %}
<a href="page/{{page}}">{{page}}</a>
<a href="{% url 'board:board_detail' board.url page %}">{{page}}</a>
{% endif %}
{% endfor %}
]
{% if current_page < last_page %}
<a href="/{{board.url}}/page/{{ current_page|add:"1" }}">{% localize on %}Next{% endlocalize %}</a>
{% with next_page=current_page|add:"1" %}
<a href="{% url 'board:board_detail' board.url next_page %}">{% localize on %}Next{% endlocalize %}</a>
{% endwith %}
{% endif %}
</div>
{% endblock content %}

View File

@@ -3,6 +3,7 @@ from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.views.generic import DetailView
from django.views.generic.edit import CreateView
from django.urls import reverse
from board.models import Post, Board
from board.forms import PostForm, ReplyForm
@@ -54,8 +55,16 @@ class BoardView(CreatePostView):
slug_url_kwarg = "url"
template_name = "board/board_detail.html"
def get(self, request, *args, **kwargs):
# If the page isn't set, then redirect to the /page/1 url
if "page" not in kwargs:
return HttpResponseRedirect(
reverse("board:board_detail", kwargs={"url": kwargs["url"], "page": 1})
)
return super(BoardView, self).get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
page = self.kwargs.get("page", 1)
page = self.kwargs["page"]
if page not in range(1, self.board.max_pages + 1):
raise Http404()