Changes between Version 3 and Version 4 of AJAX/Dojo/RefactoredFormSubmit
- Timestamp:
- Feb 11, 2007, 8:59:51 AM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
AJAX/Dojo/RefactoredFormSubmit
v3 v4 1 1 2 2 3 Here's a simple recipe that worked for me. It is quite inspired by the [wiki:AjaxDojoFormSub] example, 4 except that this one is expected to function with python-cjson for receiving JSON data. 3 Here's a simple recipe that worked for me. It is quite inspired by the [wiki:AjaxDojoFormSub] example―and from [http://www.b-list.org/weblog/2006/07/31/django-tips-simple-ajax-example-part-1 this tutorial]―except that this recipe is expected to use python-cjson for receiving JSON data. 5 4 6 5 My initial objective was to display validation errors without doing a page-refresh, by using JSON … … 11 10 12 11 {{{ 12 import cjson 13 from django.http import HttpResponse 14 from django.template import loader, Context 15 13 16 def register(request): 14 # write the view here 15 pass 17 # Register or reports any (new)form-validation errors using JSON. 18 form = ExampleForm(request.POST) 19 t = loader.get_template('myform.html') 20 if not form.is_valid(): 21 # Return JSON object containing the errors object. 22 if xhr: 23 return HttpResponse(cjson.encode(form.errors), mimetype='text/javascript') 24 else: 25 # vanilla Http response fallback 26 return HttpResponse(t.render(Context({'errors' : form.errors })) 27 else: 28 # Do something when the form has been validated. 29 ... 16 30 }}} 17 31