问题
发布到我的 django 视图后,代码似乎只是中途停止。
我有一个 ajax 帖子到 Django 视图,它被硬编码以呈现特定响应(见下文)。当我发布到该视图时,它应该始终返回该响应,但由于某种原因,视图会输出所有打印语句,而不是查询或渲染。我知道我正在被我的服务器日志重定向(如下所示)。
知道什么可能导致这种行为吗?
服务器日志:
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /static/css/style.css HTTP/1.1" 304 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /static/js/map.js HTTP/1.1" 200 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /static/js/home_jquery.js HTTP/1.1" 304 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /static/js/jquery_cookie/jquery.cookie.js HTTP/1.1" 304 -
127.0.0.1 - - [26/Aug/2013 21:27:23] "GET /favicon.ico HTTP/1.1" 404 -
I've been posted. Here are my values
<QueryDict: {u'name': [u'Mission Chinese Food'], u'address': [u'154 Orchard Street Manhattan']}>
Mission Chinese Food
154 Orchard Street Manhattan
Fish
127.0.0.1 - - [26/Aug/2013 21:27:32] "POST /results/ HTTP/1.1" 200 -
简单的 Django 视图:
def results(request):
if request.method == 'POST':
print "I've been posted. Here are my values"
print request.POST
print request.POST.get('name')
print request.POST.get('address')
restaurant = Restaurant.objects.filter(name='Fish', address='280 Bleecker St')[0]
print restaurant
return render(request, "stamped/restaurant.html", {'restaurant': restaurant}
简单的ajax帖子:
var send_data = { 'name': place.name, 'address': address};
var csrftoken = $.cookie('csrftoken');
alert(csrftoken);
alert(send_data);
function csrfSafeMethod(method) {
// these HTTP methods do not require CSRF protection
return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}
$.ajaxSetup({
crossDomain: false, // obviates need for sameOrigin test
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type)) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
}
});
$.ajax({ url: '/results/',
type: 'POST',
data: send_data,
success: function(response) {
console.log("everything worked!");
},
error: function(obj, status, err) { alert(err); console.log(err); }
});
});