我的问题与googlemapsjavascriptapi有关。更具体的自动完成功能。在
我已经将autocomplete函数作为一个搜索栏添加到我的测试站点中,这个功能很好,但是现在我想将所选的参数传递给Django视图。我被困在这里了。在
以下是我在模板中的内容:
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script type="text/javascript">
function initialize() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
}
google.maps.event.addDomListener(window, 'load', initialize);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
});
</script>
<form method="post" action="">{% csrf_token %}
<input id="searchTextField" type="text" placeholder="Enter a location" autocomplete="on">
<input type="submit" value="Search" class="postfix button"/>
</form>
在我看来:
^{pr2}$我想做的是将变量'place'传递到我的Django视图中的变量'location'(var place应该包含lat和lon,我想在Geodjango中进一步使用它)。request.POST
给出了以下内容:<QueryDict: {u'csrfmiddlewaretoken': [u'4xp3nNwzeITb1zHhkBkSGlZSPtGwmuMB']}>
但是这个我不能使用。在
是否可以将var place的内容传递到django视图?在
谢谢你的反馈!在
编辑: 我目前使用geopy的解决方法:
模板:
<input id="searchTextField" name="searchTextField" type="text" placeholder="Enter a location" autocomplete="on">
视图:
if request.method == 'POST':
location = request.POST.get('searchTextField')
gn = geocoders.Google()
place, (lat, lng) = gn.geocode(location)
仍在寻找访问script
中的var place
的方法。这可能吗?在
首先在搜索输入中添加一个
name
属性。假设您将其命名为searchTextField
,就像id
。然后你可以做request.POST.get('searchTextField')
。在相关问题 更多 >
编程相关推荐