Permalink
Browse files

Generate dynamic titles for /search/ pages

Examples include:

* “python” in items
* “python” in items in 2005
* Blogmarks in Feb, 2005
* Blogmarks tagged security in Feb, 2005
* Entries tagged startups in Feb
* “python” in quotations tagged ruby, python in 2007

That last one is at https://simonwillison.net/search/?q=python&tag=ruby&tag=python&type=quotation&year=2007
  • Loading branch information...
simonw committed Oct 8, 2017
1 parent 0d233af commit 995f3149312043d876f3b36e659e9b0d11520824
Showing with 29 additions and 2 deletions.
  1. +27 −0 blog/views.py
  2. +2 −2 templates/search.html
View
@@ -1,3 +1,4 @@
# coding=utf8
from django.shortcuts import render, get_object_or_404
from django.utils.dates import MONTHS_3_REV
from django.utils.timezone import utc
@@ -535,8 +536,34 @@ def make_queryset(klass, type_name):
if value
}
# Dynamic title
noun = {
'quotation': u'Quotations',
'blogmark': u'Blogmarks',
'entry': u'Entries',
}.get(selected.get('type')) or u'Items'
title = noun
if q:
title = u'%s” in %s' % (q, title.lower())
if selected.get('tags'):
title += u' tagged %s' % (u', '.join(selected['tags']))
datebits = []
if selected.get('month_name'):
datebits.append(selected['month_name'])
if selected.get('year'):
datebits.append(selected['year'])
if datebits:
title += u' in %s' % (u', '.join(datebits))
if not q and not selected:
title = u'Search'
return render(request, 'search.html', {
'q': q,
'title': title,
'results': results,
'total': paginator.count,
'page': page,
View
@@ -1,12 +1,12 @@
{% extends "item_base.html" %}
{% block title %}Search{% if q %} for “{{ q }}”{% endif %}{% endblock %}
{% block title %}{{ title }}{% endblock %}
{% block rel_canonical %}{% endblock %}
{% block item_content %}
{% load blog_tags %}
<h2>Search{% if q %} for “{{ q }}”{% endif %}</h2>
<h2>{{ title }}</h2>
<form action="{{ request.path }}" method="GET">
<input type="search" class="search-input" name="q" value="{{ q }}" style="width: 80%">

0 comments on commit 995f314

Please sign in to comment.