【发布时间】:2016-01-13 13:36:03
【问题描述】:
我的知识有很多空白,所以我可能试图以错误的方式解决这个问题。目标是使用这个Google Maps JavaScript API 自动完成和解析地址输入,并将结果保存在模型中。
我找不到将变量从 JS 直接传递给 view.py 的方法,所以现在计划是让 JS 填充隐藏字段 as suggested by a previous unsolved question。
ModelForm 生成的字段可以用于此目的吗?如果没有,应该如何创建表单,让JS填写字段,字段隐藏且用户不可更改,并且使用csrf_token?
这就是我目前所拥有的一切。有一堆 input 字段由 JS 自动填充。 ModelForm 生成的 form|crispy 不会被填充,即使 id 相同(models.py 中的字段名称与 Google 的命名方案相同)。我不明白 JS 的最后一段是如何工作的,这可能是我卡住的地方。
更新: 下面的代码没有给出POST,为什么?
<!-- language: lang-js -->
$.ajax({
type: 'POST',
url: 'http://localhost:8000/place/new',
dataType: 'json',
data: {
csrfmiddlewaretoken: '{{ csrf_token }}',
address: 'test'
},
success: function (response, textStatus, error) {
console.log('SUCCESS')
},
error: function (request, textStatus, error) {
console.log('error')
console.log(place)
}
});
<!-- end snippet -->
<!-- language: lang-python -->
def placenew(request):
if request.method == 'POST':
test = request.POST.get('address')
response_data = {}
print (test)
print ("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
return render(request, 'app/placenew.html', {'key':key})
else:
print ("not POST")
return render(request, 'app/placenew.html', {'key':key})
<!-- end snippet -->
【问题讨论】:
标签: javascript django google-maps