Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Few improvements to FormPreview, thanks Andy!

This commit adds several new hooks in backwards-compatible way:
 * get_initial to specify initial data based on request
 * get_auto_id to enable different AUTO_ID values
 * get_context for overriding and extending default context contents

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14659 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 70b3ac1f07b60dd1b6888b71fe20596fa64527dc 1 parent b8a7880
Honza Král authored November 21, 2010
33  django/contrib/formtools/preview.py
@@ -51,17 +51,17 @@ def unused_name(self, name):
51 51
 
52 52
     def preview_get(self, request):
53 53
         "Displays the form"
54  
-        f = self.form(auto_id=AUTO_ID)
  54
+        f = self.form(auto_id=self.get_auto_id(), initial=self.get_initial(request))
55 55
         return render_to_response(self.form_template,
56  
-            {'form': f, 'stage_field': self.unused_name('stage'), 'state': self.state},
  56
+            self.get_context(request, f),
57 57
             context_instance=RequestContext(request))
58 58
 
59 59
     def preview_post(self, request):
60 60
         "Validates the POST data. If valid, displays the preview page. Else, redisplays form."
61  
-        f = self.form(request.POST, auto_id=AUTO_ID)
62  
-        context = {'form': f, 'stage_field': self.unused_name('stage'), 'state': self.state}
  61
+        f = self.form(request.POST, auto_id=self.get_auto_id())
  62
+        context = self.get_context(request, f)
63 63
         if f.is_valid():
64  
-            self.process_preview(request, f, context) 
  64
+            self.process_preview(request, f, context)
65 65
             context['hash_field'] = self.unused_name('hash')
66 66
             context['hash_value'] = self.security_hash(request, f)
67 67
             return render_to_response(self.preview_template, context, context_instance=RequestContext(request))
@@ -91,7 +91,7 @@ def _check_security_hash(self, token, request, form):
91 91
 
92 92
     def post_post(self, request):
93 93
         "Validates the POST data. If valid, calls done(). Else, redisplays form."
94  
-        f = self.form(request.POST, auto_id=AUTO_ID)
  94
+        f = self.form(request.POST, auto_id=self.get_auto_id())
95 95
         if f.is_valid():
96 96
             if not self._check_security_hash(request.POST.get(self.unused_name('hash'), ''),
97 97
                                              request, f):
@@ -99,11 +99,30 @@ def post_post(self, request):
99 99
             return self.done(request, f.cleaned_data)
100 100
         else:
101 101
             return render_to_response(self.form_template,
102  
-                {'form': f, 'stage_field': self.unused_name('stage'), 'state': self.state},
  102
+                self.get_context(request, f),
103 103
                 context_instance=RequestContext(request))
104 104
 
105 105
     # METHODS SUBCLASSES MIGHT OVERRIDE IF APPROPRIATE ########################
106 106
 
  107
+    def get_auto_id(self):
  108
+        """
  109
+        Hook to override the ``auto_id`` kwarg for the form. Needed when
  110
+        rendering two form previews in the same template.
  111
+        """
  112
+        return AUTO_ID
  113
+
  114
+    def get_initial(self, request):
  115
+        """
  116
+        Takes a request argument and returns a dictionary to pass to the form's
  117
+        ``initial`` kwarg when the form is being created from an HTTP get.
  118
+        """
  119
+        return {}
  120
+
  121
+    def get_context(self, request, form):
  122
+        "Context for template rendering."
  123
+        return {'form': form, 'stage_field': self.unused_name('stage'), 'state': self.state}
  124
+
  125
+
107 126
     def parse_params(self, *args, **kwargs):
108 127
         """
109 128
         Given captured args and kwargs from the URLconf, saves something in
9  django/contrib/formtools/tests/__init__.py
@@ -11,6 +11,13 @@
11 11
 
12 12
 
13 13
 class TestFormPreview(preview.FormPreview):
  14
+    def get_context(self, request, form):
  15
+        context = super(TestFormPreview, self).get_context(request, form)
  16
+        context.update({'custom_context': True})
  17
+        return context
  18
+
  19
+    def get_initial(self, request):
  20
+        return {'field1': 'Works!'}
14 21
 
15 22
     def done(self, request, cleaned_data):
16 23
         return http.HttpResponse(success_string)
@@ -59,6 +66,8 @@ def test_form_get(self):
59 66
         response = self.client.get('/test1/')
60 67
         stage = self.input % 1
61 68
         self.assertContains(response, stage, 1)
  69
+        self.assertEquals(response.context['custom_context'], True)
  70
+        self.assertEquals(response.context['form'].initial, {'field1': 'Works!'})
62 71
 
63 72
     def test_form_preview(self):
64 73
         """

0 notes on commit 70b3ac1

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