Make titles more relevant

This commit is contained in:
Gabriel Augendre 2021-04-18 09:33:02 +02:00
parent 373512e52d
commit 4c3b799113
2 changed files with 9 additions and 2 deletions

View file

@ -12,11 +12,11 @@
<style>.pagination{display:flex;justify-content:space-between}</style>
{% endblock %}
{% block title %}{% endblock %}
{% block title %}{% if view.html_title %}{{ view.html_title }} | {% endif %}{% endblock %}
{% block content %}
<section>
<h2 id="blog-posts">{% block main_title %}Blog posts{% endblock %}</h2>
<h2 id="blog-posts">{% block main_title %}{{ view.main_title }}{% endblock %}</h2>
{% block search_bar %}{% endblock %}
{% for article in articles %}
<p>

View file

@ -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):