mirror of
https://github.com/django/django.git
synced 2024-12-27 11:35:53 +00:00
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
This commit is contained in:
parent
b8a78804b5
commit
70b3ac1f07
@ -51,15 +51,15 @@ class FormPreview(object):
|
|||||||
|
|
||||||
def preview_get(self, request):
|
def preview_get(self, request):
|
||||||
"Displays the form"
|
"Displays the form"
|
||||||
f = self.form(auto_id=AUTO_ID)
|
f = self.form(auto_id=self.get_auto_id(), initial=self.get_initial(request))
|
||||||
return render_to_response(self.form_template,
|
return render_to_response(self.form_template,
|
||||||
{'form': f, 'stage_field': self.unused_name('stage'), 'state': self.state},
|
self.get_context(request, f),
|
||||||
context_instance=RequestContext(request))
|
context_instance=RequestContext(request))
|
||||||
|
|
||||||
def preview_post(self, request):
|
def preview_post(self, request):
|
||||||
"Validates the POST data. If valid, displays the preview page. Else, redisplays form."
|
"Validates the POST data. If valid, displays the preview page. Else, redisplays form."
|
||||||
f = self.form(request.POST, auto_id=AUTO_ID)
|
f = self.form(request.POST, auto_id=self.get_auto_id())
|
||||||
context = {'form': f, 'stage_field': self.unused_name('stage'), 'state': self.state}
|
context = self.get_context(request, f)
|
||||||
if f.is_valid():
|
if f.is_valid():
|
||||||
self.process_preview(request, f, context)
|
self.process_preview(request, f, context)
|
||||||
context['hash_field'] = self.unused_name('hash')
|
context['hash_field'] = self.unused_name('hash')
|
||||||
@ -91,7 +91,7 @@ class FormPreview(object):
|
|||||||
|
|
||||||
def post_post(self, request):
|
def post_post(self, request):
|
||||||
"Validates the POST data. If valid, calls done(). Else, redisplays form."
|
"Validates the POST data. If valid, calls done(). Else, redisplays form."
|
||||||
f = self.form(request.POST, auto_id=AUTO_ID)
|
f = self.form(request.POST, auto_id=self.get_auto_id())
|
||||||
if f.is_valid():
|
if f.is_valid():
|
||||||
if not self._check_security_hash(request.POST.get(self.unused_name('hash'), ''),
|
if not self._check_security_hash(request.POST.get(self.unused_name('hash'), ''),
|
||||||
request, f):
|
request, f):
|
||||||
@ -99,11 +99,30 @@ class FormPreview(object):
|
|||||||
return self.done(request, f.cleaned_data)
|
return self.done(request, f.cleaned_data)
|
||||||
else:
|
else:
|
||||||
return render_to_response(self.form_template,
|
return render_to_response(self.form_template,
|
||||||
{'form': f, 'stage_field': self.unused_name('stage'), 'state': self.state},
|
self.get_context(request, f),
|
||||||
context_instance=RequestContext(request))
|
context_instance=RequestContext(request))
|
||||||
|
|
||||||
# METHODS SUBCLASSES MIGHT OVERRIDE IF APPROPRIATE ########################
|
# METHODS SUBCLASSES MIGHT OVERRIDE IF APPROPRIATE ########################
|
||||||
|
|
||||||
|
def get_auto_id(self):
|
||||||
|
"""
|
||||||
|
Hook to override the ``auto_id`` kwarg for the form. Needed when
|
||||||
|
rendering two form previews in the same template.
|
||||||
|
"""
|
||||||
|
return AUTO_ID
|
||||||
|
|
||||||
|
def get_initial(self, request):
|
||||||
|
"""
|
||||||
|
Takes a request argument and returns a dictionary to pass to the form's
|
||||||
|
``initial`` kwarg when the form is being created from an HTTP get.
|
||||||
|
"""
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def get_context(self, request, form):
|
||||||
|
"Context for template rendering."
|
||||||
|
return {'form': form, 'stage_field': self.unused_name('stage'), 'state': self.state}
|
||||||
|
|
||||||
|
|
||||||
def parse_params(self, *args, **kwargs):
|
def parse_params(self, *args, **kwargs):
|
||||||
"""
|
"""
|
||||||
Given captured args and kwargs from the URLconf, saves something in
|
Given captured args and kwargs from the URLconf, saves something in
|
||||||
|
@ -11,6 +11,13 @@ success_string = "Done was called!"
|
|||||||
|
|
||||||
|
|
||||||
class TestFormPreview(preview.FormPreview):
|
class TestFormPreview(preview.FormPreview):
|
||||||
|
def get_context(self, request, form):
|
||||||
|
context = super(TestFormPreview, self).get_context(request, form)
|
||||||
|
context.update({'custom_context': True})
|
||||||
|
return context
|
||||||
|
|
||||||
|
def get_initial(self, request):
|
||||||
|
return {'field1': 'Works!'}
|
||||||
|
|
||||||
def done(self, request, cleaned_data):
|
def done(self, request, cleaned_data):
|
||||||
return http.HttpResponse(success_string)
|
return http.HttpResponse(success_string)
|
||||||
@ -59,6 +66,8 @@ class PreviewTests(TestCase):
|
|||||||
response = self.client.get('/test1/')
|
response = self.client.get('/test1/')
|
||||||
stage = self.input % 1
|
stage = self.input % 1
|
||||||
self.assertContains(response, stage, 1)
|
self.assertContains(response, stage, 1)
|
||||||
|
self.assertEquals(response.context['custom_context'], True)
|
||||||
|
self.assertEquals(response.context['form'].initial, {'field1': 'Works!'})
|
||||||
|
|
||||||
def test_form_preview(self):
|
def test_form_preview(self):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user