我正在尝试通过 django 中的 ajax 发送模型数据。
我的view.py是这样的。
from django.core import serializers
def country(request):
country = NewTable.objects.get(id=1)
data = serializers.serialize('json',country)
return HttpResponse(data,mimetype='application/json')
我的url.py是这样的
from django.conf.urls import patterns, url
from myapp import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^country/$', views.country)
)
我的模板是这样的
function Country(){
$.get('/myapp/country', function(data) {
alert("hello");
}, "json" );
}
当我通过 View 发送字符串时,我现在收到警报消息,当我发送模型数据时,我没有收到任何警报消息。问题是什么?
最佳答案
我也遇到过类似的问题,我不再使用 django 的序列化程序,而是改用 json.dumps,现在一切正常。
查看.py
import json
def country(request):
country = NewTable.objects.filter(id=1).values()
return HttpResponse(json.dumps(country),content_type='application/json')
编辑:我不确定您可以使用 get() 作为 value(),我在代码中使用过滤器,这可能就是您的代码不起作用的原因
关于jquery - Django中通过AJAX发送模型数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23533435/