50👍
It seems like your client is posting JSON rather than formencoded data. Instead of accessing request.POST
, use request.body
(request.raw_post_data
in versions 1.3 or less) and use json.loads()
to convert to a dict.
12👍
Maybe this doesn’t fully apply to you. But when I searched for this, your question was the first Stackoverflow question.
I just wanted to get basic POST data out in Django. So just using GET worked fine for me. As the others stated, it might be easier to better format whatever script is creating the query.
Basically I have a AJAX doing a post towards Django, the POST looks a bit like this:
params = name=somename&data=abcdefg
http.send(params);
then in my view.py, i did this :
def somefuntion(request):
if request.method == 'POST':
log.info('POST applied')
alldata=request.POST
log.debug(alldata)
data = alldata.get("data", "0")
name = alldata.get("name", "0")
log.info("POST name: " + name)
log.info("POST data: " + data)
The output of alldata was :
<QueryDict: {u'data': [u'abcdefg'], u'name': [u'somename']}>
and the get commands give :
name: somename
data: abcdefg
- [Django]-How can I skip a migration with Django migrate command?
- [Django]-Django fix Admin plural
- [Django]-Django returns 403 error when sending a POST request
9👍
This works for multiple values:
dict(MyDict.lists())
Dict keys are query vars, and dict values are lists of query values.
- [Django]-Serving favicon.ico with Django. Why does settings.MEDIA_URL with django.views.generic.simple.redirect_to only work on dev environment?
- [Django]-Aggregate() vs annotate() in Django
- [Django]-How can I add a button into django admin change list view page
3👍
You can use – request.data.get("content")
This will give you the data directly if in front end (jQuery,Angular) you have NOT used JSON.stringify(data)
.
- [Django]-How to catch A 'UNIQUE constraint failed' 404 in django
- [Django]-Django Rest Framework – Could not resolve URL for hyperlinked relationship using view name "user-detail"
- [Django]-Paginating the results of a Django forms POST request
3👍
Encountered similar case recently where data from POST call was in QueryDict
form,
I was able to access value of particular key using
qdict.get(keyname)
Also I was able to iterate using QueryDict.items()
to get tuple of key & value
for q in qdict.items():
print(q)
Note that QueryDict
is subclass of Dictionary
so all standard methods of Dictionary
can also be implemented on QueryDict
.
More information available on djangoproject documentation
https://docs.djangoproject.com/en/3.0/ref/request-response/#django.http.QueryDict
- [Django]-Django Left Outer Join
- [Django]-Where to put Django startup code?
- [Django]-What's the reason why Django has SmallIntegerField?
0👍
this one works also for values that are lists:
dict_ = {k: qdict.getlist(k) if len(qdict.getlist(k))>1 else v for k, v in qdict.items()}
- [Django]-No module named urls
- [Django]-Modulus % in Django template
- [Django]-Django: request.GET and KeyError
0👍
I just experienced the same problem. It happened when I was trying to have my request data be held in request.POST instead of request.body. I was using Django with a trusted backend route already configured to work with request.POST
In order to send the data within request.POST with the fetch API I had to use ‘Content-Type’: ‘application/x-www-form-urlencoded; charset=UTF-8’ which turned my JSON stringifed data into a format like above.
In learning how to serialize data from Daniel’s answer I came across $.param(data), a jquery function which should format it correctly for you.
- [Django]-Simple way for QuerySet union and subtraction in django?
- [Django]-NoReverseMatch Error
- [Django]-Get list of Cache Keys in Django
-2👍
You can use values_list on query dict, i.e.:
MyDict.values_list('id')
– it will return list of lists with id inside. To return list of id use MyDict.values_list(‘id’, flat=True)
Useful links:
https://docs.djangoproject.com/en/1.4/ref/models/querysets/#values-list
https://docs.djangoproject.com/en/1.4/ref/models/querysets/#values
- [Django]-Loading fixtures in django unit tests
- [Django]-Django rest framework: query parameters in detail_route
- [Django]-Display only some of the page numbers by django pagination