From 4c3b7991132bca9017b5943cd1817f77823ba07d Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 18 Apr 2021 09:33:02 +0200 Subject: [PATCH] Make titles more relevant --- articles/templates/articles/article_list.html | 4 ++-- articles/views/html.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/articles/templates/articles/article_list.html b/articles/templates/articles/article_list.html index 6de2a18..13b5abd 100644 --- a/articles/templates/articles/article_list.html +++ b/articles/templates/articles/article_list.html @@ -12,11 +12,11 @@ {% endblock %} -{% block title %}{% endblock %} +{% block title %}{% if view.html_title %}{{ view.html_title }} | {% endif %}{% endblock %} {% block content %}
-

{% block main_title %}Blog posts{% endblock %}

+

{% block main_title %}{{ view.main_title }}{% endblock %}

{% block search_bar %}{% endblock %} {% for article in articles %}

diff --git a/articles/views/html.py b/articles/views/html.py index 3ffbd34..255b40f 100644 --- a/articles/views/html.py +++ b/articles/views/html.py @@ -15,6 +15,8 @@ class BaseArticleListView(generic.ListView): model = Article context_object_name = "articles" paginate_by = 10 + main_title = "Blog posts" + html_title = "" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -58,6 +60,7 @@ class ArticlesListView(PublicArticleListView): class SearchArticlesListView(PublicArticleListView): template_name = "articles/article_search.html" + html_title = "Search" def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) @@ -69,6 +72,7 @@ class SearchArticlesListView(PublicArticleListView): search_expression = self.request.GET.get("s") if not search_expression: return queryset.none() + self.html_title = f"Search results for {search_expression}" search_terms = search_expression.split() return queryset.filter( reduce(operator.and_, (Q(title__icontains=term) for term in search_terms)) @@ -89,9 +93,12 @@ class SearchArticlesListView(PublicArticleListView): class TagArticlesListView(PublicArticleListView): tag = None + main_title = None + html_title = None def dispatch(self, request, *args, **kwargs): self.tag = get_object_or_404(Tag, slug=self.kwargs.get("slug")) + self.main_title = self.html_title = f"{self.tag.name} articles" return super().dispatch(request, *args, **kwargs) def get_context_data(self, **kwargs):