Fixed #10640 - Adds a add_form_template option to ModelAdmin. Thanks jcsackett.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12216 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2010-01-12 23:34:03 +00:00
parent 09b585e4bb
commit 31f3a8c1ad
5 changed files with 27 additions and 8 deletions

View File

@ -200,6 +200,7 @@ class ModelAdmin(BaseModelAdmin):
inlines = [] inlines = []
# Custom templates (designed to be over-ridden in subclasses) # Custom templates (designed to be over-ridden in subclasses)
add_form_template = None
change_form_template = None change_form_template = None
change_list_template = None change_list_template = None
delete_confirmation_template = None delete_confirmation_template = None
@ -618,8 +619,12 @@ class ModelAdmin(BaseModelAdmin):
'save_on_top': self.save_on_top, 'save_on_top': self.save_on_top,
'root_path': self.admin_site.root_path, 'root_path': self.admin_site.root_path,
}) })
if add and self.add_form_template is not None:
form_template = self.add_form_template
else:
form_template = self.change_form_template
context_instance = template.RequestContext(request, current_app=self.admin_site.name) context_instance = template.RequestContext(request, current_app=self.admin_site.name)
return render_to_response(self.change_form_template or [ return render_to_response(form_template or [
"admin/%s/%s/change_form.html" % (app_label, opts.object_name.lower()), "admin/%s/%s/change_form.html" % (app_label, opts.object_name.lower()),
"admin/%s/change_form.html" % app_label, "admin/%s/change_form.html" % app_label,
"admin/change_form.html" "admin/change_form.html"

View File

@ -712,11 +712,20 @@ view. Templates can override or extend base admin templates as described in
If you don't specify this attribute, a default template shipped with Django If you don't specify this attribute, a default template shipped with Django
that provides the standard appearance is used. that provides the standard appearance is used.
.. attribute:: ModelAdmin.add_form_template
Path to a custom template that will be used by the model object creation
views. Templates can override or extend base admin templates as described in
`Overriding Admin Templates`_.
If you don't specify this attribute, a default template shipped with Django
that provides the standard appearance is used.
.. attribute:: ModelAdmin.change_form_template .. attribute:: ModelAdmin.change_form_template
Path to a custom template that will be used by both the model object creation Path to a custom template that will be used by the model object change views.
and change views. Templates can override or extend base admin templates as Templates can override or extend base admin templates as described in
described in `Overriding Admin Templates`_. `Overriding Admin Templates`_.
If you don't specify this attribute, a default template shipped with Django If you don't specify this attribute, a default template shipped with Django
that provides the standard appearance is used. that provides the standard appearance is used.

View File

@ -110,6 +110,7 @@ class CustomArticleAdmin(admin.ModelAdmin):
""" """
change_list_template = 'custom_admin/change_list.html' change_list_template = 'custom_admin/change_list.html'
change_form_template = 'custom_admin/change_form.html' change_form_template = 'custom_admin/change_form.html'
add_form_template = 'custom_admin/add_form.html'
object_history_template = 'custom_admin/object_history.html' object_history_template = 'custom_admin/object_history.html'
delete_confirmation_template = 'custom_admin/delete_confirmation.html' delete_confirmation_template = 'custom_admin/delete_confirmation.html'

View File

@ -550,11 +550,11 @@ class AdminViewPermissionsTest(TestCase):
self.assert_("var hello = 'Hello!';" in request.content) self.assert_("var hello = 'Hello!';" in request.content)
self.assertTemplateUsed(request, 'custom_admin/change_list.html') self.assertTemplateUsed(request, 'custom_admin/change_list.html')
# Test custom change form template # Test custom add form template
request = self.client.get('/test_admin/admin/admin_views/customarticle/add/') request = self.client.get('/test_admin/admin/admin_views/customarticle/add/')
self.assertTemplateUsed(request, 'custom_admin/change_form.html') self.assertTemplateUsed(request, 'custom_admin/add_form.html')
# Add an article so we can test delete and history views # Add an article so we can test delete, change, and history views
post = self.client.post('/test_admin/admin/admin_views/customarticle/add/', { post = self.client.post('/test_admin/admin/admin_views/customarticle/add/', {
'content': '<p>great article</p>', 'content': '<p>great article</p>',
'date_0': '2008-03-18', 'date_0': '2008-03-18',
@ -563,7 +563,10 @@ class AdminViewPermissionsTest(TestCase):
self.assertRedirects(post, '/test_admin/admin/admin_views/customarticle/') self.assertRedirects(post, '/test_admin/admin/admin_views/customarticle/')
self.failUnlessEqual(CustomArticle.objects.all().count(), 1) self.failUnlessEqual(CustomArticle.objects.all().count(), 1)
# Test custom delete and object history templates # Test custom delete, change, and object history templates
# Test custom change form template
request = self.client.get('/test_admin/admin/admin_views/customarticle/1/')
self.assertTemplateUsed(request, 'custom_admin/change_form.html')
request = self.client.get('/test_admin/admin/admin_views/customarticle/1/delete/') request = self.client.get('/test_admin/admin/admin_views/customarticle/1/delete/')
self.assertTemplateUsed(request, 'custom_admin/delete_confirmation.html') self.assertTemplateUsed(request, 'custom_admin/delete_confirmation.html')
request = self.client.get('/test_admin/admin/admin_views/customarticle/1/history/') request = self.client.get('/test_admin/admin/admin_views/customarticle/1/history/')

View File

@ -0,0 +1 @@
{% extends "admin/change_form.html" %}