I use angularJS $http send data to django and the method is POST
this is my controller.js
registerApp.controller('registerCtrl', function($scope, $http) {
$scope.submitRegisterForm = function() {
if ($scope.registerForm.$valid) {
console.log($scope.user);
$http({
method: 'POST',
url: '.',
data: $scope.user,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
} else {
return console.log(3333);
}
};
});
this is my views.py in django
if request.method == 'POST':
print request.POST
but the result of print is
<QueryDict: {u'{"username":"bob","password1":"123123","password2":"123123","email":"cot#q.com"}': [u'']}>
why the data is a Key of the dict?
and wher is the [u''] come from??
Just because you've called it form-encoded data, doesn't mean it actually is. In fact it looks like you're posting JSON. Drop that header and get the data by doing json.loads(request.body).
Related
User Data
let final_data = {
'personal_data': {
'first_name': data.first_name,
'password': data.password,
'phone': data.phone,
'username': data.username,
'email': data.email,
'image': data.profile[0]
},
'address_data': {
"address_1": data.address_1,
"address_2": data.address_2,
"city": data.city,
"pin_code": data.pin_code,
"state": data.state,
"country": data.country
}
}
Above Code is my form data which i have passed.
I had also tried to pass this data using json.stringify still it not works
AXIOS POST Request
import API from "../Axios/AxiosService"
const registerUser = (data) => API.post(`registration/`, data)
export default { registerUser }
HEADERS
if (config.url = 'registration/') {
requestOptions['headers'] = {'Content-Type': 'multipart/form-data;boundary=----WebKitFormBoundaryyrV7KO0BoCBuDbTL', 'Accept' : 'application/json'}
} else {
requestOptions['headers'] = {'Content-Type': 'application/json'}
}
if (localStorage.getItem("access_token")) {
requestOptions['headers'] = {"Authorization": "Token " + localStorage.getItem("access_token")}
}
Above Code is AXIOS POST Request and also Passed headers accrodingly. Using Application/Json i get all data except image and from data i also tried to pass data.profile instead of data.profile[0] but it didn't works.
Django Rest Framework error
**In POST method of Django Rest Framework i had used below listed ways to get but in all methods i didn't get any data **
request.POST
request.data
request.Files
You should give the queryset to UserRegistrationView like;
class UserRegistrationView(generics.CreateAPIView):
queryset = User.objects.all()
serialier_class = UserSerializer
In React inspite of passing Content-type="multipart/form-data" in to POST request it will not pass image object in backend.
So we have to convert whole data in to formdata and then pass in POST Request.
That's how I solved my error.
Have a look at the Example
Background:
Working in a Django application, I have a template that performs an action (makes a payement) using an external API. The API call is done in Javascript.
On Success, the API returns a response object. This works well on tests.
function makePayment(applicationAmount) {
let paymentEngine = RmPaymentEngine.init({
key: 'abcd'
firstName: '{{ user.f_name }}',
onSuccess: function(response) {
console.log('callback Successful Response', response);
// TODO: Add a function to post the payment records to the database
// response from the API contains a dictionary like (read json) object
// how can I get that response object in Python and use those response
// object values to update a django model database records
},
onError: function(response) {
console.log('callback Error Response', response);
// TODO: Add a function to throw an error message
},
onClose: function() {
console.log("closed");
}
});
}
Question:
How can I get that onSuccess response object variables in Python and use those response object values to update a django model database records?
Looked at this link: How do I return the response from an asynchronous call? but can't seem to implement my need.
I'd be happy to be directed to a simple resource that explains the procedure, not something very involved that takes hours to understand.
Using ideas from #Lag11 and #Arount and a wonderful tutorial here, I created 2 function based views, one to just serve the 'page' the other to handle the 'page post to DB'.
Summary, I did this in the template:
function makePayment(applicationAmount) {
let paymentEngine = RmPaymentEngine.init({
key: 'abcd'
firstName: '{{ user.f_name }}',
onSuccess: function(response) {
console.log('callback Successful Response', response);
// start new additions
data = {
"payer_email": "{{ user.email }}",
"payer_phone": "{{ user.phone }}",
"payment_amount_total": response.amount,
}
$.ajax({
type: 'POST',
url: "{% url 'post_purchase_form' %}",
data: data,
onSuccess: function (response) {
console.log('callback db post Successful', response);
},
error: function (response) {
// alert the error if any error occured
alert(response);
}
})
// end new additions
},
onError: function(response) {
console.log('callback Error Response', response);
// TODO: Add a function to throw an error message
},
onClose: function() {
console.log("closed");
}
});
}
And this in the views.py:
def application_form_view(request):
user = request.user
form = ApplicationForm(instance=user)
return render(request, 'application_form.html', {'form': form, 'user': user})
def post_application_form_view(request):
# request should be ajax and method should be POST.
if request.is_ajax and request.method == "POST":
# get the form data
form = ApplicationForm(request.POST, request.FILES)
# save the data and after fetch the object in instance
if form.is_valid():
instance = form.save()
# serialize in new FormPurchase object in json
ser_instance = serializers.serialize('json', [ instance, ])
# send to client side.
return JsonResponse({"instance": ser_instance}, status=200)
else:
# some form errors occured.
return JsonResponse({"error": form.errors}, status=400)
# some error occured
return JsonResponse({"error": "unknown errors"}, status=400)
I'm trying to post data to a django view using ajax. I have no errors just there is no data returned.
Here is my javascript ajax:
function PostGoal(){
console.log('POSTGOAL!!!')
data_s = {
'csrfmiddlewaretoken': $('input[name="csrfmiddlewaretoken"]').val(),
'goal': { 'name':'gg','box':'sasa' }
}
$.ajax({
method: "POST",
url: "http://127.0.0.1:8000/",//"{% url 'home' %}",//"/",//"{% url 'home' %}",
contentType: 'application/json',
//data: JSON.stringify(data_s),
data: {'QQww': "1"},
//dataType: 'json',//expected type of response
success: function (data) {
console.log('aa'+JSON.stringify(data))
},
error: function(xhr,errmsg,err){
console.log('err: '+JSON.stringify(err)+' msg:'+errmsg)
}
});
}
here is my view function:
def analyzer(request):
if request.method == 'POST':
post_data = request.POST
print(post_data)
print (' ajax:',request.is_ajax())
The boolean request.is_ajax() is ajax is always False request.data or request.POST.data do not exist and same for GET: request.GET is empty. However I can see in the log in the terminal :
[22/Mar/2018 22:15:26] "GET /?QQww=1 HTTP/1.1" 200 9746
so essentially the data are parsed in the url? Thanks in advance.
I read in some other posts that 'method' was the right field to specify the call. For some reason, the parameter 'method' is NOT working and the correct field is 'type':
$.ajaxSetup({
headers: { "X-CSRFToken": '{{csrf_token}}' }
});
$.ajax({
type: "POST",
url: "http://127.0.0.1:8000/",//"{% url 'home' %}",//"/",//"{% url 'home' %}",
contentType: 'application/json',
//data: JSON.stringify(data_s),
data: {'QQww': "1"},
//dataType: 'json',//expected type of response
success: function (data) {
console.log('aa'+JSON.stringify(data))
},
error: function(xhr,errmsg,err){
console.log('err: '+JSON.stringify(err)+' msg:'+errmsg)
}
});
The html button also needs to be of type 'button' not 'submit':
<button type="button" class="btn btn-primary btn-large" id="yesbtn" name="yesbtn" value="yesbtn" onclick="PostGoal();">Yes ยป</button>
I also added the ajax setup for the csrf token to correct the error:
'Forbidden (CSRF token missing or incorrect.): /'
I hope it helps.
This is what My ajax call looks like
$.ajax({
url:"{% url 'handsontable' %}",
data: {'getdata': JSON.stringify(hot.getData())},
dataType: 'json',
type: 'POST',
success: function (res, status) {
alert(res);
alert(status);
},
error: function (res) {
alert(res.status);
}
});
This is what my django view looks like.
if request.method == 'POST':
request_getdata = request.POST.get('getdata', 'None')
return HttpResponse(request_getdata)
The alerts in ajax return the data and "success". But my HttpResponse returns "None".
Any idea why it is not passing the data through? Thanks!
First off you are trying to POST to a html file
url:"/utility_tool/decisions/solution_options/handsontable.html",
Instead, it should be a url to a view.
Second, the ajax post request should have the csrftoken in it's header and you can set it up like this:
<script type="text/javascript">
// using jQuery get csrftoken from your HTML
var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
beforeSend: function (xhr, settings) {
// if not safe, set csrftoken
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
$.ajax({
url: "{% url 'name of the view from urls.py' %}",
data: {
// here getdata should be a string so that
// in your views.py you can fetch the value using get('getdata')
'getdata': JSON.stringify(hot.getData())
},
dataType: 'json',
success: function (res, status) {
alert(res);
alert(status);
},
error: function (res) {
alert(res.status);
}
});
</script>
And in your django view:
# views.py
from django.http import JsonResponse
def someView(request):
if request.method == 'POST':
# no need to do this
# request_csrf_token = request.POST.get('csrfmiddlewaretoken', '')
request_getdata = request.POST.get('getdata', None)
# make sure that you serialise "request_getdata"
return JsonResponse(request_getdata)
And in your urls:
# urls.py
urlpatterns = [
# other urls
path('some/view/', views.someView, name='name of the view in urls.py'),
]
I cannot add comments because I do not yet have up to 50 reputations as demanded by StackOverflow. This is supposed to be a comment under the answer provided by #abybaddi009. He has done a very good job thus far but the answer needs a finishing touch.
In the view
request_getdata = request.POST.get('getdata', None) does not work
but this does
body = request.body.decode('utf-8')
data = body[3]
request.body.decode('utf-8') returns a string which would look something like getdata=your_data you can then use string manipulation techniques or regex to extract your data.
What you need to do is :
code for ajax call ( in js file) to send the data to the view
jQuery.ajax(
{
'url': "url_pattern_in_urls_py_file/",
'type': 'POST',
'contentType': 'application/json; charset=UTF-8',
'data': JSON.stringify({'updated_data':your_data_val}),
'dataType': 'json',
'success': function ( return_data ) {
//success body
}
}
);
code in django view with respect to above POST ajax call to receive the data
import json
if request.method == 'POST':
updatedData=json.loads(request.body.decode('UTF-8'))
I added return false; at the end of the ajax request and it worked. I printed out the values in the view instead of using HttpResponse.
I have this code in AngularJS:
myApp.controller('LoginCtrl', function($scope, $http){
$scope.formData = {};
$scope.doLogin = function(url, pass){
$http({
url: url,
method: "POST",
data: $.param($scope.formData)
}).success(function(data) {
console.log(data)
});
}
});
And in the beckend (Flask), I have this:
def user_authenticate():
login = request.args.get('login')
password = request.args.get('password')
print login, password
The problem is that request.args come empty.
UPDATE
After have a lot of problems with this, I solve using another Stackoverflow answer. So, I got this code:
ANGULARJS
$scope.doLogin = function(url, pass){
$http({
url: url,
method: "POST",
headers: { 'Content-Type': 'application/json' },
data: JSON.stringify(data)
}).success(function(data) {
console.log(data)
});
}
FLASK
def view_of_test():
post = request.get_json()
param = post.get('param_name')
OLD VERSION
I just figured that I need to change my code to this:
AngularJS:
myApp.controller('LoginCtrl', function($scope, $http){
$scope.formData = {};
$scope.doLogin = function(url, pass){
$http({
url: url,
method: "POST",
data: $.param($scope.formData),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(data) {
console.log(data)
});
}
});
Just include the 'Content-Type' header.
Flask:
def user_authenticate():
login = request.form.get('login')
password = request.form.get('password')
print login, password
Instead of use request.args, use request.form.