赋值之前引用的Django局部变量“ form”

时间:2019-05-24 04:21:03

标签: django

我正在尝试制作一个表单,在该表单中,用户输入州的位置,并返回该位置的天气

一切正常,直到我在代码中添加了 cities = City.objects.all()

从django.shortcuts导入

汇入要求 来自.models import City

def索引(请求):         城市= City.objects.all()#返回数据库中的所有城市

    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=ec2052730c7fdc28b89a0fbfe8560346'

    if request.method == 'POST': # only true if form is submitted
            form = CityForm(request.POST) # add actual request data to form for processing
    form.save() # will validate and save if validate

    form = CityForm()
    weather_data = []

    for city in cities:

            city_weather = requests.get(url.format(city)).json() #request the API data and convert the JSON to Python data types

            weather = {
            'city' : city,
            'temperature' : city_weather['main']['temp'],
            'description' : city_weather['weather'][0]['description'],
            'icon' : city_weather['weather'][0]['icon']
            }

            weather_data.append(weather) #add the data for the current city into our list

    context = {'weather_data' : weather_data, 'form' : form}
    return render(request, 'weathers/index.html', context)
  

/之前引用的/局部变量'form'处的UnboundLocalError   分配请求方法:GET请求网址:http://127.0.0.1:8000/   Django版本:   2.2.1异常类型:UnboundLocalError异常值:赋值之前引用的本地变量“ form”异常位置:   索引第12行中的C:\ Users \ Admin \ Desktop \ the_weather \ weathers \ views.py   Python可执行文件:   C:\ Users \ Admin \ AppData \ Local \ Programs \ Python \ Python37-32 \ python.exe   Python版本:   3.7.3 Python路径:['C:\ Users \ Admin \ Desktop \ the_weather','C:\ Users \ Admin \ AppData \ Local \ Programs \ Python \ Python37-32 \ python37.zip',   'C:\ Users \ Admin \ AppData \ Local \ Programs \ Python \ Python37-32 \ DLLs',   'C:\ Users \ Admin \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib',   'C:\ Users \ Admin \ AppData \ Local \ Programs \ Python \ Python37-32',   'C:\ Users \ Admin \ AppData \ Roaming \ Python \ Python37 \ site-packages',   'C:\ Users \ Admin \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages']   服务器时间:2019年5月24日星期五04:09:08 +0000

2 个答案:

答案 0 :(得分:0)

您需要更改:

    if request.method == 'POST': # only true if form is submitted
            form = CityForm(request.POST) # add actual request data to form for processing
    form.save() # will validate and save if validate
    form = CityForm()

       if request.method == 'POST': # only true if form is submitted
            form = CityForm(request.POST) # add actual request data to form for processing
            if form.is_valid():
                 form.save() # will validate and save if validate
                 # having form in same scope when there is a post request
       else:
           form = CityForm()

答案 1 :(得分:0)

您在这里犯错了

if request.method == 'POST': # only true if form is submitted
            form = CityForm(request.POST) # add actual request data to form for processing
    form.save() # will validate and save if validate

    form = CityForm()

如果请求是GET,则在分配之前直接转到form.save()

要解决这个问题

if request.method == 'POST':
    form = CityForm(request.POST)
    form.save()

form = CityForm()