Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 31f3a8c1ad3b6ec33c503241484107bf35cdadba 1 parent 09b585e
Jannis Leidel authored January 12, 2010
7  django/contrib/admin/options.py
@@ -200,6 +200,7 @@ class ModelAdmin(BaseModelAdmin):
200 200
     inlines = []
201 201
 
202 202
     # Custom templates (designed to be over-ridden in subclasses)
  203
+    add_form_template = None
203 204
     change_form_template = None
204 205
     change_list_template = None
205 206
     delete_confirmation_template = None
@@ -618,8 +619,12 @@ def render_change_form(self, request, context, add=False, change=False, form_url
618 619
             'save_on_top': self.save_on_top,
619 620
             'root_path': self.admin_site.root_path,
620 621
         })
  622
+        if add and self.add_form_template is not None:
  623
+            form_template = self.add_form_template
  624
+        else:
  625
+            form_template = self.change_form_template
621 626
         context_instance = template.RequestContext(request, current_app=self.admin_site.name)
622  
-        return render_to_response(self.change_form_template or [
  627
+        return render_to_response(form_template or [
623 628
             "admin/%s/%s/change_form.html" % (app_label, opts.object_name.lower()),
624 629
             "admin/%s/change_form.html" % app_label,
625 630
             "admin/change_form.html"
15  docs/ref/contrib/admin/index.txt
@@ -712,11 +712,20 @@ view. Templates can override or extend base admin templates as described in
712 712
 If you don't specify this attribute, a default template shipped with Django
713 713
 that provides the standard appearance is used.
714 714
 
  715
+.. attribute:: ModelAdmin.add_form_template
  716
+
  717
+Path to a custom template that will be used by the model object creation
  718
+views. Templates can override or extend base admin templates as described in
  719
+`Overriding Admin Templates`_.
  720
+
  721
+If you don't specify this attribute, a default template shipped with Django
  722
+that provides the standard appearance is used.
  723
+
715 724
 .. attribute:: ModelAdmin.change_form_template
716 725
 
717  
-Path to a custom template that will be used by both the model object creation
718  
-and change views. Templates can override or extend base admin templates as
719  
-described in `Overriding Admin Templates`_.
  726
+Path to a custom template that will be used by the model object change views.
  727
+Templates can override or extend base admin templates as described in
  728
+`Overriding Admin Templates`_.
720 729
 
721 730
 If you don't specify this attribute, a default template shipped with Django
722 731
 that provides the standard appearance is used.
1  tests/regressiontests/admin_views/models.py
@@ -110,6 +110,7 @@ class CustomArticleAdmin(admin.ModelAdmin):
110 110
     """
111 111
     change_list_template = 'custom_admin/change_list.html'
112 112
     change_form_template = 'custom_admin/change_form.html'
  113
+    add_form_template = 'custom_admin/add_form.html'
113 114
     object_history_template = 'custom_admin/object_history.html'
114 115
     delete_confirmation_template = 'custom_admin/delete_confirmation.html'
115 116
 
11  tests/regressiontests/admin_views/tests.py
@@ -550,11 +550,11 @@ def testCustomModelAdminTemplates(self):
550 550
         self.assert_("var hello = 'Hello!';" in request.content)
551 551
         self.assertTemplateUsed(request, 'custom_admin/change_list.html')
552 552
 
553  
-        # Test custom change form template
  553
+        # Test custom add form template
554 554
         request = self.client.get('/test_admin/admin/admin_views/customarticle/add/')
555  
-        self.assertTemplateUsed(request, 'custom_admin/change_form.html')
  555
+        self.assertTemplateUsed(request, 'custom_admin/add_form.html')
556 556
 
557  
-        # Add an article so we can test delete and history views
  557
+        # Add an article so we can test delete, change, and history views
558 558
         post = self.client.post('/test_admin/admin/admin_views/customarticle/add/', {
559 559
             'content': '<p>great article</p>',
560 560
             'date_0': '2008-03-18',
@@ -563,7 +563,10 @@ def testCustomModelAdminTemplates(self):
563 563
         self.assertRedirects(post, '/test_admin/admin/admin_views/customarticle/')
564 564
         self.failUnlessEqual(CustomArticle.objects.all().count(), 1)
565 565
 
566  
-        # Test custom delete and object history templates
  566
+        # Test custom delete, change, and object history templates
  567
+        # Test custom change form template
  568
+        request = self.client.get('/test_admin/admin/admin_views/customarticle/1/')
  569
+        self.assertTemplateUsed(request, 'custom_admin/change_form.html')
567 570
         request = self.client.get('/test_admin/admin/admin_views/customarticle/1/delete/')
568 571
         self.assertTemplateUsed(request, 'custom_admin/delete_confirmation.html')
569 572
         request = self.client.get('/test_admin/admin/admin_views/customarticle/1/history/')
1  tests/templates/custom_admin/add_form.html
... ...
@@ -0,0 +1 @@
  1
+{% extends "admin/change_form.html" %}

0 notes on commit 31f3a8c

Please sign in to comment.
Something went wrong with that request. Please try again.