2016-11-01 3 views
0

좋아요, 나는 꽤 장고에 익숙하지 않고 아주 간단한 앱을 만들고있었습니다. 사용자가 텍스트 상자에 위치를 입력하면 Google지도에서 해당 위치의 좌표를 가져 와서 다음 페이지에 인쇄합니다. 지금까지 JSON 파일에서 다른 많은 정보와 함께 좌표를 얻을 수있었습니다. 나는 또한 위도와 경도를 성공적으로 분리하여 별개의 변수로 저장했습니다. 나는 그들을 인쇄 할 수 없습니다. 하나의 값만 인쇄됩니다. 누군가가 이걸 도와 줄 수 있니? 이것은 내가 지금까지 쓴 것입니다 : 내 views.py :하나의 Django HttpResponse에서 두 개의 변수를 반환하는 방법은 무엇입니까?

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 

내 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> 
+0

이 두 값을 한 문자열을 만들고 넣을 것 'httpResponse()'에서 사용하거나 다른 html 파일과'lat, lng'를'render()'와 html로'{{lat}}','{{lng}}') 사용하십시오 – furas

+0

만약 있다면 렌더링을 사용하려면 새 템플릿과 새 URL을 만들어야합니다. 끈 아이디어는 더 나을 것이다. – DeA

답변

0

그것이

# 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") 
+0

이 경우에는 django.http.JsonResponse - https://docs.djangoproject.com/en/1.10/ref/request-response/#id4를 사용하는 것이 좋습니다. –

관련 문제