【发布时间】:2017-02-24 20:26:49
【问题描述】:
我在 Django 上创建了不同的基于类的视图。在我创建的 HTML 上,一些表单使用 AJAX 发出请求。我的问题是它给了我
方法不允许 (POST)
我不知道我这样做是否正确,或者我是否需要修改某些内容才能使其正常工作。
我的 view.py 是这样的
class Landing(View):
def get(self,request):
if request.method == 'POST':
if request.is_ajax():
data = {"lat":20.586, "lon":-89.530}
print request.POST.get('value')
return JsonResponse(data)
return render(request,'landing.html',{'foo':'bar'})
我从 Javascript 发送请求
$(document).ready(function() {
$('#productos').on('change', function(e) {
//Call the POST
e.preventDefault();
var csrftoken = getCookie('csrftoken');
var value = $('#productos').val();
$.ajax({
url: window.location.href,
type: "POST",
data: {
csrfmiddlewaretoken : csrftoken,
value : value
},
success : function(json) {
console.log(json);
drop(json);
},
error : function(xhr,errmsg,err){
console.log(xhr.status+": "+xhr.responseText)
}
});
});
});
我从网上获得了一些代码,但我真的不知道如何使用它,因为他们使用它时没有基于类的视图。
那么,我的代码需要什么才能接受 POST 方法?
【问题讨论】:
标签: javascript python django