Reputation: 11
I try to change the admin interface. I have added a field to change the status of the Order in my shop application via Ajax.
I base on this Django snippet Admin list_display Ajax.
This is my JavaScript code:
$(document).ready(function() {
$('.order-helper').css("margin", "0");
$('.order-helper').blur(function(){
var input = $(this);
$.ajax({
url: "/ajax-status-update/"+ input.attr('name') +"/",
data: order=input.attr('value'),
type: "POST",
complete: function(xhr_obj, msg){
if (msg == 'success') {
input.css("border", "1px solid green");
} else {
input.css("border", "1px solid red");
}
},
});
});
});
This is my view:
def order_helper(request, prod_id):
prod = get_object_or_404(FixedOrder, id=prod_id)
prod.status = request.POST.get('order')
prod.save()
return HttpResponse(content='Ok', status=200)
The view works with not Ajax call.
I read tons of articles about this and nothing works for me. I know that it is connected somehow with CSRF. In the console I get a 403 error.
I read that I have to add this JavaScript snippet anywhere:
$(document).ajaxSend(function(event, xhr, settings) {
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
function sameOrigin(url) {
// url could be relative or scheme relative or absolute
var host = document.location.host; // host + port
var protocol = document.location.protocol;
var sr_origin = '//' + host;
var origin = protocol + sr_origin;
// Allow absolute or scheme relative URLs to same origin
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
// or any other URL that isn't scheme relative or absolute i.e relative.
!(/^(\/\/|http:|https:).*/.test(url));
}
function safeMethod(method) {
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
});
But if I add this I get 500 in the console. How can I check what kind of problem it is? Maybe I miss a CSRF token?
Upvotes: 0
Views: 2088
Reputation: 32070
Your data
parameter to $.ajax
is not escaped properly. The heart of your problem probably lies there:
$.ajax({
url: "/ajax-status-update/"+ input.attr('name') +"/",
data: "order=" + input.attr('value'),
type: "POST",
complete: function(xhr_obj, msg){
if (msg == 'success') {
input.css("border", "1px solid green");
} else {
input.css("border", "1px solid red");
}
},
});
If you want to continue to use the CSRF middleware, you should probably serialize a form instead:
data: $('#theForm').serialize(),
Upvotes: 1