是Django的新手,一切进展顺利,直到此...
当用户单击地图时,我试图将纬度/经度传递给Django表单。
这是我的地图页面的js:
Android 4.3
还有我的 forms.py
#standardSQL
SELECT id,
ARRAY(
SELECT CAST(val AS STRING) FROM t.array_1 val UNION ALL
SELECT * FROM t.array_2
) concatanated_arrays
FROM joined_table t
和 views.py
for(int i=0;i<array.length();i++){
for(int j=i;j<array.length();j++){}
}
任何对此的帮助将不胜感激!谢谢!!!
答案 0 :(得分:0)
最简单的方法是将经/纬度作为查询参数传递给表单,例如:
// I was in a hurry, but make sure to url encode these
function openForm(lat, lon){
window.location.href = 'http://127.0.0.1:8000/form?lat=' + lat + '&lon=' + lon;
alert("latitude: "+lat+" and longitude: "+lon);
}
现在,在您看来,您必须从request
解压缩这些值,然后使用这些值初始化表单。您可以将初始值添加到django form with initial
argument中。我们使用latitude
和longitude
作为字段名称,因为它们是MyForm
定义中使用的字段名称。我们使用lat
和lon
访问查询参数,因为它们是我们在javascript中使用的名称。
def mineral(request):
form = None
if request.method == "POST":
form = MineralForm(request.POST) # populates form with what user tried to submit...
if form.is_valid():
post = form.save(commit=False)
post.author = request.user
post.created_date = timezone.now()
post.save()
return render(request, 'blog/matMap.html')
if form is None:
lat = request.GET.get('lat')
lon = request.GET.get('lon')
if lat is not None and lon is not None:
form = MyForm(initial={ 'latitude': lat, 'longitude': lon })
else:
form = MyForm()
return render(request, 'form.html', {'form': form})