Ok, I'm pretty new to django and I was building a very simple app. User enters location into a textbox, I fetch the co-ordinates for that location from google maps and print it in the next page. So far, I have been able to get the co-ordinates in a JSON file along with a lot of other information. I have also successfully isolated the latitudes and longitudes and stored them into separate variables. I can't get them to print though. Only one value gets printed. Could someone help me with this? This is what I've written so far: My views.py:
好吧,我是django的新手,我正在构建一个非常简单的应用程序。用户在文本框中输入位置,我从谷歌地图获取该位置的坐标并在下一页打印。到目前为止,我已经能够获得JSON文件中的坐标以及许多其他信息。我还成功地隔离了纬度和经度,并将它们存储在单独的变量中。我不能让他们打印。只打印一个值。有人可以帮我吗?这是我到目前为止所写的:我的views.py:
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
import requests
import json
# Create your views here.
def homepage(request):
if request.method == 'GET':
return render(request, 'geog/homepage.html')
if request.method == 'POST':
string = request.POST['location']
maps_url = "http://maps.googleapis.com/maps/api/geocode/json?address="+string
json_string = requests.get(maps_url).content
json_dict = json.loads(json_string)
lat = json_dict['results'][0]['geometry']['location']['lat']
lng = json_dict['results'][0]['geometry']['location']['lng']
return HttpResponse(lat, lng) #It only prints lat, does not print lng
My Html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>geog</title>
</head>
<body>
<form action="" method="POST"> {% csrf_token %}
<input type="text" name='location'/>
<button type="submit">submit</button>
</form>
</body>
</html>
1 个解决方案
#1
0
it would be
这将是
# this will return a json object with a lat and lng property which have the desired values
return HttpResponse(json.dumps({"lat":lat, "lng":lng}), content_type="application/json")
#1
0
it would be
这将是
# this will return a json object with a lat and lng property which have the desired values
return HttpResponse(json.dumps({"lat":lat, "lng":lng}), content_type="application/json")