diff options
| author | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-10-26 08:10:18 +0100 |
|---|---|---|
| committer | Saptak Sengupta <saptak013@gmail.com> | 2025-10-27 12:14:57 +0530 |
| commit | a111cf8661307c29d727d72827cefa9cb842146f (patch) | |
| tree | c962994d6f9559f65b25d76ce16ae6bb1a3a2202 /blog | |
| parent | 8d230390ceb35c892cc9110968412f42f8811f3c (diff) | |
Set HTML meta description of blog entries.
Diffstat (limited to 'blog')
| -rw-r--r-- | blog/models.py | 10 | ||||
| -rw-r--r-- | blog/tests.py | 35 |
2 files changed, 42 insertions, 3 deletions
diff --git a/blog/models.py b/blog/models.py index 988c607e..bceb067d 100644 --- a/blog/models.py +++ b/blog/models.py @@ -182,6 +182,12 @@ class Entry(models.Model): def pub_date_localized(self): return date_format(self.pub_date) + @property + def description(self): + return _("Posted by {author} on {pub_date}").format( + author=self.author, pub_date=self.pub_date_localized + ) + def save(self, *args, **kwargs): self.summary_html = ContentFormat.to_html(self.content_format, self.summary) self.body_html = ContentFormat.to_html(self.content_format, self.body) @@ -208,9 +214,7 @@ class Entry(models.Model): tags = { "og:type": "article", "og:title": self.headline, - "og:description": _("Posted by {author} on {pub_date}").format( - author=self.author, pub_date=self.pub_date_localized - ), + "og:description": self.description, "og:article:published_time": self.pub_date.isoformat(), "og:article:author": self.author, "og:image": static("img/logos/django-logo-negative.png"), diff --git a/blog/tests.py b/blog/tests.py index b5c1b180..c345fda5 100644 --- a/blog/tests.py +++ b/blog/tests.py @@ -190,6 +190,41 @@ class EventTestCase(DateTimeMixin, TestCase): class ViewsTestCase(DateTimeMixin, TestCase): + def test_detail_view_html_meta(self): + headline = "Pride and Prejudice - Review" + author = "Jane Austen" + pub_date = date(2005, 7, 21) + blog_entry = Entry.objects.create( + pub_date=pub_date, + is_active=True, + headline=headline, + slug="a", + author=author, + ) + blog_description = "Posted by Jane Austen on July 21, 2005" + self.assertEqual(blog_entry.description, blog_description) + + blog_url = blog_entry.get_absolute_url() + response = self.client.get(blog_url) + self.assertEqual(response.status_code, 200) + + expected_html_meta_tags = [ + f'<meta name="description" content="{blog_description}" />', + '<meta property="og:type" content="article" />', + f'<meta property="og:title" content="{headline}" />', + f'<meta property="og:description" content="{blog_description}" />', + '<meta property="og:article:published_time" content="2005-07-21T00:00:00" />', + f'<meta property="og:article:author" content="{author}" />', + '<meta property="og:image:alt" content="Django logo" />', + f'<meta property="og:url" content="{blog_url}" />', + '<meta property="og:site_name" content="Django Project" />', + '<meta property="twitter:card" content="summary" />', + '<meta property="twitter:creator" content="djangoproject" />', + '<meta property="twitter:site" content="djangoproject" />', + ] + for expected_html_meta_tag in expected_html_meta_tags: + self.assertContains(response, expected_html_meta_tag, html=True) + def test_staff_with_change_permission_can_see_unpublished_detail_view(self): """ Staff users with change permission on BlogEntry can't see unpublished entries |
