Current page tweaks

* Add link rel canonical to all pages (/ points itself instead of /?page=current-season because Google crawls duplicate versions of pages less often. I want / to be crawled frequently, so it has to be the canonical version of the content it holds)
* Fix a bug where passing an invald page slug would mean that no page was highlighted as current in the footer
This commit is contained in:
Matthias Portzel 2022-05-12 08:16:44 -05:00
parent 7f7d21384e
commit 40fd0aaa5c
3 changed files with 21 additions and 12 deletions

View File

@ -6,7 +6,7 @@ import sys
def main(): def main():
"""Run administrative tasks.""" """Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'whispermaphone.settings') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "whispermaphone.settings")
try: try:
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
except ImportError as exc: except ImportError as exc:

View File

@ -12,6 +12,11 @@
{% block head %} {% block head %}
<link rel="alternate" href="/feed" type="application/rss+xml" title="RSS"> <link rel="alternate" href="/feed" type="application/rss+xml" title="RSS">
{% if not first_page %}
<link rel="canonical" href="/?page={{ current_page_slug }}">
{% else %}
<link rel="canonical" href="/">
{% endif %}
<link href="{% static 'thoughts/codehighlight.css' %}" rel="stylesheet"> <link href="{% static 'thoughts/codehighlight.css' %}" rel="stylesheet">
{% endblock %} {% endblock %}
@ -21,7 +26,7 @@
<nav class="history-nav top" aria-label="History Navigation"> <nav class="history-nav top" aria-label="History Navigation">
<ul> <ul>
{% for page in pages %} {% for page in pages %}
{% if page.slug == current_page %} {% if page.slug == current_page_slug %}
<li><span class="current-page">{{ page.formatted_name }}</span></li> <li><span class="current-page">{{ page.formatted_name }}</span></li>
{% else %} {% else %}
<li><a href="?page={{ page.slug }}">{{ page.formatted_name }}</a></li> <li><a href="?page={{ page.slug }}">{{ page.formatted_name }}</a></li>
@ -91,7 +96,7 @@
<nav class="history-nav bottom" aria-label="History Navigation"> <nav class="history-nav bottom" aria-label="History Navigation">
<ul> <ul>
{% for page in pages %} {% for page in pages %}
{% if page.slug == current_page %} {% if page.slug == current_page_slug %}
<li><span class="current-page">{{ page.formatted_name }}</span></li> <li><span class="current-page">{{ page.formatted_name }}</span></li>
{% else %} {% else %}
<li><a href="?page={{ page.slug }}">{{ page.formatted_name }}</a></li> <li><a href="?page={{ page.slug }}">{{ page.formatted_name }}</a></li>

View File

@ -35,12 +35,10 @@ def index(request):
except ValueError: except ValueError:
highlighted_uuid = "" highlighted_uuid = ""
# Figure out what page we're viewing ## Figure out what page we're viewing
pages = get_all_pages() pages = get_all_pages()
# First item in pages should be listed first requested_slug = request.GET.get("page", default="")
requested_page = pages[0]
requested_slug = request.GET.get("page", default=requested_page.slug)
# show=uuid takes priority over page # show=uuid takes priority over page
if highlighted_uuid: if highlighted_uuid:
@ -50,10 +48,16 @@ def index(request):
except Thought.DoesNotExist: except Thought.DoesNotExist:
pass pass
if requested_page.slug != requested_slug: # When we get here, either:
for p in pages: # no slug was passed, requested_slug is ""
if p.slug == requested_slug: # a valid highlighted_uuid was passed, requested_slug is the slug of that page
requested_page = p # requested_slug is an invalid slug
# requested_slug is a valid slug
# First item in pages should be listed first
requested_page = pages[0]
for p in pages:
if p.slug == requested_slug:
requested_page = p
thoughts = requested_page.get_all_entries() thoughts = requested_page.get_all_entries()
@ -62,7 +66,7 @@ def index(request):
"highlighted": highlighted_uuid, "highlighted": highlighted_uuid,
"authenticated": authenticated, "authenticated": authenticated,
"pages": pages, "pages": pages,
"current_page": requested_slug, "current_page_slug": requested_page.slug,
"first_page": requested_page == pages[0] # if you're viewing the first page "first_page": requested_page == pages[0] # if you're viewing the first page
}) })