1👍
Instead of using ‘serialize’, you could use serializeArray(). I find working with JSON is easier.
<script> // Submitting address form via Ajax
function addressform() {
var data = $('#addressform').serializeArray();
$.post( '/suggestions', $.toJSON(data) );
}
</script>
And then, in the Django view
import json
def receiveSuggestions(request):
data = json.loads(request.body)
#
# Add to database and other stuff
#
return HttpResponse( json.dumps({"status" : 1}) )
Basically, request.body in the view will give you access to whatever has been sent in the post request. Earlier it was raw_post_data. Maybe this will help? Where's my JSON data in my incoming Django request?
1👍
Based on your question, it sounds like you are looking to process the data passed by your ajax request from django.
You can look into the request.POST
for post requests or request.GET
for get requests to get a dictionary of the key-values you passed. See the documentation here.
To provide an example, I’ll you have a view handler for /addressform/
, let’s call it address_form
def address_form(request):
if request.method == 'POST':
# process the post and return appropriate response
params = request.POST
door_no = params['DoorNo']
#etc for each key value
...
else:
# get on the url, handle return the appropriate response
- [Answered ]-Post method in Django models
- [Answered ]-How to get latest timestamp from the two columns in django
- [Answered ]-Passing data to bootstrap modal Django template
- [Answered ]-Python Cursor SELECT columns dynamically
Source:stackexchange.com