Trying to make a POST call to a Django view

33 views
Skip to first unread message

carloratm

unread,
May 2, 2016, 12:01:47 PM5/2/16
to Django users
hello everyone,
I am a frontend developer studying Django, so please forgive my noobish question...
I am trying to make a simple ajax call working.

I have a form in a template. I would like to use that form to save an object into the database (asynchronously).
On form submit event:
* I prevent regular action
* I serialize form data in a JSON
* I pass that object to a view using a jquery POST
* The view receive the JSON data
* The view use the request.POST data to save an object into the database.
* The view returns a message.

Is that flow straightforward, or am I completely missing the goal?

Now some code and the error I got.

The relevant part of template:



And this is the error I got:


I noticed that in the view the request.POST is *empty*!
That is wrong, I suppose.

I am surely missing something.
Any help is really appreciated.

Thank you in advance,
carlo

ludovic coues

unread,
May 2, 2016, 12:12:35 PM5/2/16
to django...@googlegroups.com
I would use the browser developer tools to look at what JQuery is
sending to django in the submit event.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/44f6da2a-c677-4485-99f2-eec20f8dfe0f%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

Carlo Ascani

unread,
May 3, 2016, 12:45:04 AM5/3/16
to django...@googlegroups.com
2016-05-02 18:11 GMT+02:00 ludovic coues <cou...@gmail.com>:
> I would use the browser developer tools to look at what JQuery is
> sending to django in the submit event.

http://dpaste.com/2MQTNZP

I do not see anything wrong there...

Any ideas?
Thank you
> You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/pRkBho_TZrQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAEuG%2BTb4q_-OwNX%2BMWbp2DQrRB0-gdSv5-rD-h31GYrSWHXwew%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.



--
Carlo Ascani | www.carlorat.me
carloratm@freenode

Jani Tiainen

unread,
May 3, 2016, 1:44:14 AM5/3/16
to django...@googlegroups.com
No that isn't. If jquery sets content-type, to application/json Django
doesn't put that data in .POST, but it's in .body

Django only puts in .POST form-data (either urlencoded or multipart)

> I am surely missing something.
> Any help is really appreciated.
>

If you want to send JSON data with ajax, using something like django-nap
or django-rest-framework is a great help.

--

Jani Tiainen
Reply all
Reply to author
Forward
0 new messages