Code

Changes between Version 3 and Version 4 of AJAX/Dojo/RefactoredFormSubmit


Ignore:
Timestamp:
02/11/07 06:59:51 (7 years ago)
Author:
erob@…
Comment:

added register view (initial rev)

Legend:

Unmodified
Added
Removed
Modified
  • AJAX/Dojo/RefactoredFormSubmit

    v3 v4  
    11 
    22 
    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. 
     3Here'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. 
    54 
    65My initial objective was to display validation errors without doing a page-refresh, by using JSON 
     
    1110 
    1211{{{ 
     12import cjson 
     13from django.http import HttpResponse 
     14from django.template import loader, Context 
     15 
    1316def 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       ...     
    1630}}} 
    1731