Browse Source

Make titles more relevant

master
Gabriel Augendre 4 weeks ago
parent
commit
4c3b799113
  1. 4
      articles/templates/articles/article_list.html
  2. 7
      articles/views/html.py

4
articles/templates/articles/article_list.html

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

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

Loading…
Cancel
Save