266π
If you are posting JSON to Django, I think you want request.body
(request.raw_post_data
on Django < 1.4). This will give you the raw JSON data sent via the post. From there you can process it further.
Here is an example using JavaScript, jQuery, jquery-json and Django.
JavaScript:
var myEvent = {id: calEvent.id, start: calEvent.start, end: calEvent.end,
allDay: calEvent.allDay };
$.ajax({
url: '/event/save-json/',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: $.toJSON(myEvent),
dataType: 'text',
success: function(result) {
alert(result.Result);
}
});
Django:
def save_events_json(request):
if request.is_ajax():
if request.method == 'POST':
print 'Raw Data: "%s"' % request.body
return HttpResponse("OK")
Django < 1.4:
def save_events_json(request):
if request.is_ajax():
if request.method == 'POST':
print 'Raw Data: "%s"' % request.raw_post_data
return HttpResponse("OK")
91π
I had the same problem. I had been posting a complex JSON response, and I couldnβt read my data using the request.POST dictionary.
My JSON POST data was:
//JavaScript code:
//Requires json2.js and jQuery.
var response = {data:[{"a":1, "b":2},{"a":2, "b":2}]}
json_response = JSON.stringify(response); // proper serialization method, read
// http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
$.post('url',json_response);
In this case you need to use method provided by aurealus. Read the request.body and deserialize it with the json stdlib.
#Django code:
import json
def save_data(request):
if request.method == 'POST':
json_data = json.loads(request.body) # request.raw_post_data w/ Django < 1.4
try:
data = json_data['data']
except KeyError:
HttpResponseServerError("Malformed data!")
HttpResponse("Got json data")
- [Django]-Serializer call is showing an TypeError: Object of type 'ListSerializer' is not JSON serializable?
- [Django]-Many-To-Many Fields View on Django Admin
- [Django]-How to show processing animation / spinner during ajax request?
47π
Method 1
Client : Send as JSON
$.ajax({
url: 'example.com/ajax/',
type: 'POST',
contentType: 'application/json; charset=utf-8',
processData: false,
data: JSON.stringify({'name':'John', 'age': 42}),
...
});
//Sent as a JSON object {'name':'John', 'age': 42}
Server :
data = json.loads(request.body) # {'name':'John', 'age': 42}
Method 2
Client : Send as x-www-form-urlencoded
(Note: contentType
& processData
have changed, JSON.stringify
is not needed)
$.ajax({
url: 'example.com/ajax/',
type: 'POST',
data: {'name':'John', 'age': 42},
contentType: 'application/x-www-form-urlencoded; charset=utf-8', //Default
processData: true,
});
//Sent as a query string name=John&age=42
Server :
data = request.POST # will be <QueryDict: {u'name':u'John', u'age': 42}>
Changed in 1.5+ : https://docs.djangoproject.com/en/dev/releases/1.5/#non-form-data-in-http-requests
Non-form data in HTTP requests
:
request.POST will no longer include data posted via HTTP requests with
non form-specific content-types in the header. In prior versions, data
posted with content-types other than multipart/form-data or
application/x-www-form-urlencoded would still end up represented in
the request.POST attribute. Developers wishing to access the raw POST
data for these cases, should use the request.body attribute instead.
Probably related
- [Django]-Folder Structure for Python Django-REST-framework and Angularjs
- [Django]-Django template how to look up a dictionary value with a variable
- [Django]-Django 1.8 KeyError: 'manager' on relationship
28π
Its important to remember Python 3 has a different way to represent strings β they are byte arrays.
Using Django 1.9 and Python 2.7 and sending the JSON data in the main body (not a header) you would use something like:
mydata = json.loads(request.body)
But for Django 1.9 and Python 3.4 you would use:
mydata = json.loads(request.body.decode("utf-8"))
I just went through this learning curve making my first Py3 Django app!
- [Django]-Get Timezone from City in Python/Django
- [Django]-Django.contrib.auth.logout in Django
- [Django]-ImportError: No module named 'django.core.urlresolvers'
23π
request.raw_response
is now deprecated. Use request.body
instead to process non-conventional form data such as XML payloads, binary images, etc.
- [Django]-Django β limiting query results
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language
- [Django]-How to format time in django-rest-framework's serializer?
9π
on django 1.6 python 3.3
client
$.ajax({
url: '/urll/',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(json_object),
dataType: 'json',
success: function(result) {
alert(result.Result);
}
});
server
def urll(request):
if request.is_ajax():
if request.method == 'POST':
print ('Raw Data:', request.body)
print ('type(request.body):', type(request.body)) # this type is bytes
print(json.loads(request.body.decode("utf-8")))
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-Django substr / substring in templates
- [Django]-Negating a boolean in Django template
6π
Something like this. Itβs worked:
Request data from client
registerData = {
{% for field in userFields%}
{{ field.name }}: {{ field.name }},
{% endfor %}
}
var request = $.ajax({
url: "{% url 'MainApp:rq-create-account-json' %}",
method: "POST",
async: false,
contentType: "application/json; charset=utf-8",
data: JSON.stringify(registerData),
dataType: "json"
});
request.done(function (msg) {
[alert(msg);]
alert(msg.name);
});
request.fail(function (jqXHR, status) {
alert(status);
});
Process request at the server
@csrf_exempt
def rq_create_account_json(request):
if request.is_ajax():
if request.method == 'POST':
json_data = json.loads(request.body)
print(json_data)
return JsonResponse(json_data)
return HttpResponse("Error")
- [Django]-Python (and Django) best import practices
- [Django]-Are Django SECRET_KEY's per instance or per app?
- [Django]-Django MultiValueDictKeyError error, how do I deal with it
5π
The HTTP POST payload is just a flat bunch of bytes. Django (like most frameworks) decodes it into a dictionary from either URL encoded parameters, or MIME-multipart encoding. If you just dump the JSON data in the POST content, Django wonβt decode it. Either do the JSON decoding from the full POST content (not the dictionary); or put the JSON data into a MIME-multipart wrapper.
In short, show the JavaScript code. The problem seems to be there.
- [Django]-How to get the domain name of my site within a Django template?
- [Django]-How to print BASE_DIR from settings.py from django app in terminal?
- [Django]-What does 'many = True' do in Django Rest FrameWork?
- [Django]-How to save pillow image object to Django ImageField?
- [Django]-What's the best solution for OpenID with Django?
- [Django]-Django :How to integrate Django Rest framework in an existing application?
2π
html code
file name : view.html
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#mySelect").change(function(){
selected = $("#mySelect option:selected").text()
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
url: '/view/',
data: {
'fruit': selected
},
success: function(result) {
document.write(result)
}
});
});
});
</script>
</head>
<body>
<form>
<br>
Select your favorite fruit:
<select id="mySelect">
<option value="apple" selected >Select fruit</option>
<option value="apple">Apple</option>
<option value="orange">Orange</option>
<option value="pineapple">Pineapple</option>
<option value="banana">Banana</option>
</select>
</form>
</body>
</html>
Django code:
Inside views.py
def view(request):
if request.method == 'POST':
print request.body
data = request.body
return HttpResponse(json.dumps(data))
- [Django]-Remove pk field from django serialized objects
- [Django]-Django: Reference to an outer query may only be used in a subquery
- [Django]-Django: Error: You don't have permission to access that port
0π
If you have set rest_framework.parsers.JSONParser in your django settings
Then your json will be in the data attribute of request object.
To access it:
def post(self, request):
json_data = request.data
- [Django]-Django.contrib.auth.logout in Django
- [Django]-Why is __init__ module in django project loaded twice
- [Django]-Should I be adding the Django migration files in the .gitignore file?
-3π
Using Angular you should add header to request or add it to module config
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
$http({
url: url,
method: method,
timeout: timeout,
data: data,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})
- [Django]-How to get a favicon to show up in my django app?
- [Django]-Django REST Framework: how to substitute null with empty string?
- [Django]-How to delete project in django
-4π
request.POST is just a dictionary-like object, so just index into it with dict syntax.
Assuming your form field is fred, you could do something like this:
if 'fred' in request.POST:
mydata = request.POST['fred']
Alternately, use a form object to deal with the POST data.
- [Django]-ForeignKey to abstract class (generic relations)
- [Django]-Django: Arbitrary number of unnamed urls.py parameters
- [Django]-Django py.test does not find settings module