First of, when you use modelforms, you can instead do:
if form.is_valid():
    form.save()

It doesn't make any difference to Django if you post the data
via AJAX or regular form submit, the only thing you want to
make different is probably the return value (redirect or json).
This can be done using the request.is_ajax().
Ideally if you want to use AJAX, you should keep the form
working if a user has js disabled. Here .is_ajax() will help
you.

~Jakob

On 4 Jun., 08:16, Darren <blogposts.dar...@gmail.com> wrote:
> Hi
>
> I am trying to make sense of Django's form capabilities, having only
> done ajax posts before..
>
> Before ajaxification:
>
> <form action="/add_person/" method="POST">
>     {{ form.as_p }}
>     <input type="submit" value="Submit" />
> </form>
>
> def add_person(request):
>     form = PersonForm(request.POST)
>     if form.is_valid():
>         Person.objects.create(name=form.cleaned_data['name'])
>         return HttpResponseRedirect('/people/')
>
> After ajaxification:
>
> <label for="name">Name:</label>
> <input id="name" name="name" />
> <button onClick="submit(this)" type="submit">submit</button>
>
> p = Person.objects.create(name=request.POST['name'])
> data = json.dumps({ 'name': p.name })
> return HttpResponse(data, mimetype="application/javascript")
>
> So my question is, is it worth using all the Django gubbins when the
> alternative appears very simple?
>
> Thanks!
> Darren
--~--~---------~--~----~------------~-------~--~----~
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