【发布时间】:2021-08-16 19:47:38
【问题描述】:
我正在尝试在 django 中创建一个 Web 应用程序,用户可以在其中输入他的邮政编码,然后显示附近的餐馆。但是,我想不出一种方法让 django 收集来自 google api 调用的 json 响应并在我的网站上使用。我试过在网上四处寻找帮助,但我似乎无法让它们中的任何一个起作用。任何人都可以看到如何改进我的views.py,或者我是否需要在我的urls.py 中添加一些东西,或者是否有其他方法。 (我不打算将邮政编码存储在我的数据库中)
地理编码 JSON 响应示例
{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Parkway",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara County",
"short_name" : "Santa Clara County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.4267861,
"lng" : -122.0806032
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.4281350802915,
"lng" : -122.0792542197085
},
"southwest" : {
"lat" : 37.4254371197085,
"lng" : -122.0819521802915
}
}
},
"place_id" : "ChIJtYuu0V25j4ARwu5e4wwRYgE",
"plus_code" : {
"compound_code" : "CWC8+R3 Mountain View, California, United States",
"global_code" : "849VCWC8+R3"
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
Views.py
from .forms import PropertyForm
def property_add(request):
if request.method == 'GET':
form = PropertyForm(request.GET)
if form.is_valid():
new_property = form.save(commit=False)
address = new_property.postcode
url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' + "'" + address + "'" + '+SG&key=APIKEY'
res = urllib.urlopen(url).read()
data = json.loads(res)
latlon = {
'lat': geodata['results'][0]['geometry']['location']['lat'],
'lon': geodata['results'][0]['geometry']['location']['lng']
}
return Response(data, 'food/location_index.html', latlon)
else:
raise Http404
else:
raise PropertyForm()
FORMS.PY
from django import forms
class PropertyForm(forms.Form):
postcode = forms.CharField(max_length = 6)
食品/LOCATION_INDEX.HTML
{% extends 'base.html' %}
{% load static %}
{% block content %}
<h2>Food places!!!</h2>
<hr>
{% for location in locations %}
<h2><a href="{% url 'location-detail' location.pk %}">{{ location.name }}</a></h2>
{% endfor %}
<form action='' method="get">
{{ form }}
<input type="submit" value="Submit">
</form>
<p>Your current location is {{ latlon }} </p>
{% endblock content %}
【问题讨论】:
-
不清楚,您想从 google API 响应中将什么特别存储在 DB 中?
-
您好,感谢您的回复。我不打算存储响应中的任何内容。我想从 api 响应中取回用户的经纬度,并显示附近所有附近的餐馆。餐厅在我的数据库中,但我不打算将用户位置存储在我的数据库中。
-
所以你需要在你的
food/location_index.html模板中渲染lanlon。显示此模板。 -
latlon 是您的上下文,而不是键。请参阅我发布的解决方案。
-
我将其更改为 {{lon}} 和 {{lat}},但是,每当我提交表单时,它仍然没有从 json 响应中获取数据,而是在我的终端上显示:GET / location_index/?postcode=382948
标签: python json django api geocoding