hi oscar,

Thank you!!! that is awesome! i not even need to manipulate the form using
output data json!
thanks! but ,  $("xxx").live  really is important...


Regards,
MH

On Fri, May 4, 2012 at 4:56 PM, Oscar Mederos <omede...@gmail.com> wrote:

> Hello Min,
>
> On Friday, May 4, 2012, 10:14:46 AM, you wrote:
>
> > hi oscar,
>
> > how do you make use of the particular method to be able to render
> > the form only? as you said make use of the
> > https://docs.djangoproject.com/en/1.4/ref/contrib/csrf/#ajax  and
> > it able to render the form instead of page?
> > because i'm using kurtis method, if any validation error, i
> > retrieve the data from javascript and manipulate the
> > output in html only.
>
> What I usually do is the following:
> - Place  the  content  of  my  form  in a separate template file (eg.
> password-form.html)
> - Using jQuery, I do something like:
>
> //Override the behavior of the 'submit' event of the form.
> //Very important to use 'live' instead of 'click'. Otherwise,
> //if we change the HTML of the form, this function won't be triggered
> //next time we submit the form.
> $("#my-form").live('submit', function(e) {
>                e.preventDefault();
>                $.ajax({
>                    type: "post",
>                    //DRY. I suppose you already have the url where
>                    //you want to make the POST request in the "action"
>                    //tag of the <form>
>                    url: $("#my-form").attr("action"),
>                    //This automatically get all the values from the
>                    //inputs in the form (eg. a=1&b=2)
>                    data: $("#my-form").serialize(),
>                    dataType: "json",
>                    success: function(data) {
>                        //If there was an error...
>                        if (data.error == 1) {
>                            //All we have to do is replace the body of
>                            //the <form>..</form> with the new HTML
>                            //rendered value of the form returned from
>                            //the server
>                            $("#my-form").replaceWith(data.message);
>                        }
>                        else {
>                            //Do whatever you want here
>                        }
>                    }
>                });
>            });
>
> 'data' is what the view should return. What I usually do is the
> following:
> * If there was an error validating the form, then:
>  - "data.error" will be 1
>  - "data.message" will have the form rendered
> * If the form was validated without problems
>  - "data.error" will be 0
>  - "data.message" will have some success message (eg. "Your password
>  was changed successfully).
>
> - Now, in the view... how do I return the rendered form as HTML?
>
> The "password-form.html" template should look like:
>
> <form id="my-form" action="/some/url">
>      {% csrf_token %}
>      ...
> </form>
>
> And the code of the view could be something like the following (of
> course, checking that the request method was POST, etc).
>
> def view(request):
>    # Create the bounded form (as you usually do)
>    f = MyForm(request.POST)
>    if form.is_valid():
>       form.save()
>       d = {'error': 0, 'message': 'Some success message'}
>    else:
>       d = {'error': 1}
>       # Here we render the entire HTML text of the form. You can pass
>       # anything you want in the context...
>       form_html = render_to_string('password-form.html',...,
> context_instance=RequestContext(request))
>       d['message'] = form_html
>    response = simplejson.dumps(d)
>    return HttpResponse(response, mimetype='application/json')
>
> I wrote all of it in the editor of my email client, so if something
> does not work, just let me know.
>
> Again, very important to use the snippet provided in
> https://docs.djangoproject.com/en/1.4/ref/contrib/csrf/#ajax
>
>
> > Regards,
> > MH
>
>
> --
> Oscar Mederos
> omede...@gmail.com
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to