Skip to content

Commit a111cf8

Browse files
sarahboyceSaptakS
authored andcommitted
Set HTML meta description of blog entries.
1 parent 8d23039 commit a111cf8

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

blog/models.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ def is_published(self):
182182
def pub_date_localized(self):
183183
return date_format(self.pub_date)
184184

185+
@property
186+
def description(self):
187+
return _("Posted by {author} on {pub_date}").format(
188+
author=self.author, pub_date=self.pub_date_localized
189+
)
190+
185191
def save(self, *args, **kwargs):
186192
self.summary_html = ContentFormat.to_html(self.content_format, self.summary)
187193
self.body_html = ContentFormat.to_html(self.content_format, self.body)
@@ -208,9 +214,7 @@ def opengraph_tags(self):
208214
tags = {
209215
"og:type": "article",
210216
"og:title": self.headline,
211-
"og:description": _("Posted by {author} on {pub_date}").format(
212-
author=self.author, pub_date=self.pub_date_localized
213-
),
217+
"og:description": self.description,
214218
"og:article:published_time": self.pub_date.isoformat(),
215219
"og:article:author": self.author,
216220
"og:image": static("img/logos/django-logo-negative.png"),

blog/tests.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,41 @@ def test_past_future_ordering(self):
190190

191191

192192
class ViewsTestCase(DateTimeMixin, TestCase):
193+
def test_detail_view_html_meta(self):
194+
headline = "Pride and Prejudice - Review"
195+
author = "Jane Austen"
196+
pub_date = date(2005, 7, 21)
197+
blog_entry = Entry.objects.create(
198+
pub_date=pub_date,
199+
is_active=True,
200+
headline=headline,
201+
slug="a",
202+
author=author,
203+
)
204+
blog_description = "Posted by Jane Austen on July 21, 2005"
205+
self.assertEqual(blog_entry.description, blog_description)
206+
207+
blog_url = blog_entry.get_absolute_url()
208+
response = self.client.get(blog_url)
209+
self.assertEqual(response.status_code, 200)
210+
211+
expected_html_meta_tags = [
212+
f'<meta name="description" content="{blog_description}" />',
213+
'<meta property="og:type" content="article" />',
214+
f'<meta property="og:title" content="{headline}" />',
215+
f'<meta property="og:description" content="{blog_description}" />',
216+
'<meta property="og:article:published_time" content="2005-07-21T00:00:00" />',
217+
f'<meta property="og:article:author" content="{author}" />',
218+
'<meta property="og:image:alt" content="Django logo" />',
219+
f'<meta property="og:url" content="{blog_url}" />',
220+
'<meta property="og:site_name" content="Django Project" />',
221+
'<meta property="twitter:card" content="summary" />',
222+
'<meta property="twitter:creator" content="djangoproject" />',
223+
'<meta property="twitter:site" content="djangoproject" />',
224+
]
225+
for expected_html_meta_tag in expected_html_meta_tags:
226+
self.assertContains(response, expected_html_meta_tag, html=True)
227+
193228
def test_staff_with_change_permission_can_see_unpublished_detail_view(self):
194229
"""
195230
Staff users with change permission on BlogEntry can't see unpublished entries

djangoproject/templates/blog/entry_detail.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
{% block title %}{{ object.headline|escape }} | Weblog{% endblock %}
55

6+
{% block description %}{{ object.description }}{% endblock %}
7+
68
{% block og_tags %}
79
{% for property, content in object.opengraph_tags.items %}
810
<meta property="{{ property }}" content="{{ content }}" />

0 commit comments

Comments
 (0)