Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python地理编码名称';纬度';没有定义_Python_Django_Google Maps - Fatal编程技术网

Python地理编码名称';纬度';没有定义

Python地理编码名称';纬度';没有定义,python,django,google-maps,Python,Django,Google Maps,我是django的新手。我想 bulid一个webapp按地址搜索地图。(地理编码)这是my view.py,我想响应我的html文件以显示输入地址的地图。我的代码显示它在/map/处有名称错误。我不知道原因。谢谢你的回复 from django.shortcuts import render import urllib from urllib.request import urlopen import json def searchMap(request): if requ

我是django的新手。我想 bulid一个webapp按地址搜索地图。(地理编码)这是my view.py,我想响应我的html文件以显示输入地址的地图。我的代码显示它在/map/处有名称错误。我不知道原因。谢谢你的回复

from django.shortcuts import render
import urllib

from urllib.request import urlopen

import json


def searchMap(request):

     if request.method == "POST":
         global latitude
         global longitude
         city_name = request.POST.get('address')
         city_name_Url="https://maps.googleapis.com/maps/api/geocode/json?
         address"+city_name
         city_name_Url_Quote=urllib.parse.quote(city_name_Url,':?=/')
         response=urlopen(city_name_Url_Quote).read().decode('utf-8')
         response_json = json.loads(response)


         latitude = response_json.get('results')[0]['geometry']['location']['lat']
         longitude = api_response_dict('results')[0]['geometry']['location']['lng'] 

     return render(request,'WebPage1.html',{'Latitude':latitude,'Longitude':longitude})
错误消息:

名称错误位于/map/

未定义名称“latitude”请求方法:GET 请求URL:Django版本:1.8.13 异常类型:NameError异常值:名称“latitude”不是 定义的异常 位置:C:\Users\alienware\Desktop\DjangoWebProject12\DjangoWebProject12\HelloWorld\views.py 在searchMap中,第26行 可执行文件:C:\Users\alienware\Desktop\DjangoWebProject12\DjangoWebProject12\env\u DjangoWebProject2\Scripts\python.exe Python版本:3.6.3 Python路径:
['C:\Users\Desktop\DjangoWebProject12\DjangoWebProject12', 'C:\Users\Desktop\DjangoWebProject12\DjangoWebProject12', 'C:\Users\Desktop\DjangoWebProject12\DjangoWebProject12\env\u DjangoWebProject2\Scripts\python36.zip', 'C:\Users\AppData\Local\Programs\Python\Python36\DLLs', 'C:\Users\AppData\Local\Programs\Python\Python36\lib', 'C:\Users\AppData\Local\Programs\Python\Python36', 'C:\Users\Desktop\DjangoWebProject12\DjangoWebProject12\env_DjangoWebProject2', 'C:\Users\Desktop\DjangoWebProject12\DjangoWebProject12\env\u DjangoWebProject2\lib\site packages'] 服务器时间:2018年2月5日星期一21:57:22+0800

我假设(从错误回溯)您的real代码如下所示:

def searchMap(request):
    if request.method == "POST":
         # XXX totally unrelated but : __NEVER__ use mutable globals
         # in  a django app.
         global latitude
         global longitude
         city_name = request.POST.get('address')
         city_name_Url="https://maps.googleapis.com/maps/api/geocode/json?
         address"+city_name
         city_name_Url_Quote=urllib.parse.quote(city_name_Url,':?=/')
         response=urlopen(city_name_Url_Quote).read().decode('utf-8')
         response_json = json.loads(response)


        latitude = response_json.get('results')[0]['geometry']['location']['lat']
        longitude = api_response_dict('results')[0]['geometry']['location']['lng'] 

    return render(request,'WebPage1.html',{'Latitude':latitude,'Longitude':longitude})
现在问问自己,当请求的方法不是POST时会发生什么。。。是的,
if
块中的所有内容都被忽略,并且只执行最后一条语句(
return render(…)
)。此时,既没有定义
纬度
也没有定义
经度
,因此您的错误

首先要解决的是使用POST请求进行搜索。POST用于更新服务器的状态。搜索不会改变服务器的状态(至少它不应该改变,而您的确实不会改变),所以这里正确的动词是GET。作为一个额外的好处,它将使您的搜索结果页面成为书签

因此,首先更改模板代码,使用
GET
作为表单的
方法
属性的值。然后,在您看来,根本不测试request方法,而是在
request.GET
中查找querystring参数。此外,您还需要处理用户实际上没有发送任何内容的情况:

def searchMap(request):
    context = {}
    city_name = request.GET.get('address', '').strip()
    if city_name:
        # hint: use the `python-requests` module instead,
        # it will make you life much easier
        city_name_Url="https://maps.googleapis.com/maps/api/geocode/json?
        address"+city_name
        city_name_Url_Quote=urllib.parse.quote(city_name_Url,':?=/')
        response=urlopen(city_name_Url_Quote).read().decode('utf-8')
        response_json = json.loads(response)
        context["Latitude"] = response_json.get('results')[0]['geometry']['location']['lat']
        context["Longitude"] = api_response_dict('results')[0]['geometry']['location']['lng'] 
    else:
       # here you want to display an error message to
       # the user - don't forget to check the case
       # in your template. Note that it would be simpler 
       # using a Django Form...
       context["error"] = "Some errorr message here"

    return render(request,'WebPage1.html',context)

请用您得到的完整错误编辑您的问题。好的,我还没有编辑它。请修复代码段缩进,使其与实际代码完全匹配。我认为只有在执行
IF
块时才定义变量
latitude
,如果不满足条件,则未定义变量。这将导致您得到的错误。在if条件之前定义纬度。(将其设置为无或类似)感谢您的回复。我更改了它,但仍然不起作用。但我不知道如何从上下文中获取html中的纬度和经度。@ZPC与您以前的方法相同-只是现在可能无法设置纬度和经度,因此您必须签入模板(并检查“错误”键)。请注意,我发布的代码片段仍然非常脆弱-即在
urlopen
调用等方面没有错误处理,您可以确定这部分有时会中断。。。但现在你的工作是处理这件事。