Fixed #31993 -- Added subtitles to admin change/view pages.

This commit is contained in:
Jon Dufresne 2020-09-08 15:39:43 -07:00 committed by Mariusz Felisiak
parent cbe34dc8ec
commit 84609b3205
4 changed files with 59 additions and 3 deletions

View File

@ -1627,6 +1627,7 @@ class ModelAdmin(BaseModelAdmin):
context = { context = {
**self.admin_site.each_context(request), **self.admin_site.each_context(request),
'title': title % opts.verbose_name, 'title': title % opts.verbose_name,
'subtitle': str(obj) if obj else None,
'adminform': adminForm, 'adminform': adminForm,
'object_id': object_id, 'object_id': object_id,
'original': obj, 'original': obj,
@ -1815,6 +1816,7 @@ class ModelAdmin(BaseModelAdmin):
'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)}, 'selection_note': _('0 of %(cnt)s selected') % {'cnt': len(cl.result_list)},
'selection_note_all': selection_note_all % {'total_count': cl.result_count}, 'selection_note_all': selection_note_all % {'total_count': cl.result_count},
'title': cl.title, 'title': cl.title,
'subtitle': None,
'is_popup': cl.is_popup, 'is_popup': cl.is_popup,
'to_field': cl.to_field, 'to_field': cl.to_field,
'cl': cl, 'cl': cl,

View File

@ -86,6 +86,7 @@
<div id="content" class="{% block coltype %}colM{% endblock %}"> <div id="content" class="{% block coltype %}colM{% endblock %}">
{% block pretitle %}{% endblock %} {% block pretitle %}{% endblock %}
{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %} {% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}
{% block content_subtitle %}{% if subtitle %}<h2>{{ subtitle }}</h2>{% endif %}{% endblock %}
{% block content %} {% block content %}
{% block object-tools %}{% endblock %} {% block object-tools %}{% endblock %}
{{ content }} {{ content }}

View File

@ -1,6 +1,6 @@
{% extends "admin/base.html" %} {% extends "admin/base.html" %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} {% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block branding %} {% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }}</a></h1> <h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }}</a></h1>

View File

@ -100,10 +100,16 @@ class AdminViewBasicTestCase(TestCase):
cls.superuser = User.objects.create_superuser(username='super', password='secret', email='super@example.com') cls.superuser = User.objects.create_superuser(username='super', password='secret', email='super@example.com')
cls.s1 = Section.objects.create(name='Test section') cls.s1 = Section.objects.create(name='Test section')
cls.a1 = Article.objects.create( cls.a1 = Article.objects.create(
content='<p>Middle content</p>', date=datetime.datetime(2008, 3, 18, 11, 54, 58), section=cls.s1 content='<p>Middle content</p>',
date=datetime.datetime(2008, 3, 18, 11, 54, 58),
section=cls.s1,
title='Article 1',
) )
cls.a2 = Article.objects.create( cls.a2 = Article.objects.create(
content='<p>Oldest content</p>', date=datetime.datetime(2000, 3, 18, 11, 54, 58), section=cls.s1 content='<p>Oldest content</p>',
date=datetime.datetime(2000, 3, 18, 11, 54, 58),
section=cls.s1,
title='Article 2',
) )
cls.a3 = Article.objects.create( cls.a3 = Article.objects.create(
content='<p>Newest content</p>', date=datetime.datetime(2009, 3, 18, 11, 54, 58), section=cls.s1 content='<p>Newest content</p>', date=datetime.datetime(2009, 3, 18, 11, 54, 58), section=cls.s1
@ -1027,6 +1033,53 @@ class AdminViewBasicTest(AdminViewBasicTestCase):
self.assertContains(response, '<th scope="col" class="column-value">') self.assertContains(response, '<th scope="col" class="column-value">')
self.assertNotContains(response, '<th scope="col" class="sortable column') self.assertNotContains(response, '<th scope="col" class="sortable column')
def test_change_view_subtitle_per_object(self):
response = self.client.get(
reverse('admin:admin_views_article_change', args=(self.a1.pk,)),
)
self.assertContains(
response,
'<title>Article 1 | Change article | Django site admin</title>',
)
self.assertContains(response, '<h1>Change article</h1>')
self.assertContains(response, '<h2>Article 1</h2>')
response = self.client.get(
reverse('admin:admin_views_article_change', args=(self.a2.pk,)),
)
self.assertContains(
response,
'<title>Article 2 | Change article | Django site admin</title>',
)
self.assertContains(response, '<h1>Change article</h1>')
self.assertContains(response, '<h2>Article 2</h2>')
def test_view_subtitle_per_object(self):
viewuser = User.objects.create_user(
username='viewuser', password='secret', is_staff=True,
)
viewuser.user_permissions.add(
get_perm(Article, get_permission_codename('view', Article._meta)),
)
self.client.force_login(viewuser)
response = self.client.get(
reverse('admin:admin_views_article_change', args=(self.a1.pk,)),
)
self.assertContains(
response,
'<title>Article 1 | View article | Django site admin</title>',
)
self.assertContains(response, '<h1>View article</h1>')
self.assertContains(response, '<h2>Article 1</h2>')
response = self.client.get(
reverse('admin:admin_views_article_change', args=(self.a2.pk,)),
)
self.assertContains(
response,
'<title>Article 2 | View article | Django site admin</title>',
)
self.assertContains(response, '<h1>View article</h1>')
self.assertContains(response, '<h2>Article 2</h2>')
@override_settings(TEMPLATES=[{ @override_settings(TEMPLATES=[{
'BACKEND': 'django.template.backends.django.DjangoTemplates', 'BACKEND': 'django.template.backends.django.DjangoTemplates',