分配前引用的 Django 局部变量“表单”

Posted

技术标签:

【中文标题】分配前引用的 Django 局部变量“表单”【英文标题】:Django local variable 'form' referenced before assignment 【发布时间】:2019-10-10 15:43:12 【问题描述】:

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

在我在代码中添加 cities = City.objects.all() 之前它工作正常

从 django.shortcuts 导入渲染 导入请求 从 .models 导入城市

定义索引(请求): city = 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)

UnboundLocalError at / 之前引用的局部变量 'form' 分配请求方法:GET 请求URL:http://127.0.0.1:8000/ Django 版本: 2.2.1 异常类型:UnboundLocalError 异常值:赋值前引用的局部变量'form' 异常位置: C:\Users\Admin\Desktop\the_weather\weathers\views.py 在索引中,第 12 行 Python可执行文件: C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\python.exe 蟒蛇版本: 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

【问题讨论】:

【参考方案1】:

你错了

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()

【讨论】:

【参考方案2】:

你需要改变:

    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()

【讨论】:

以上是关于分配前引用的 Django 局部变量“表单”的主要内容,如果未能解决你的问题,请参考以下文章

UnboundLocalError:分配前引用的局部变量“光标”

分配前引用的 /blog/search/ 局部变量 'cd' 处的 UnboundLocalError

UnboundLocalError:分配前引用的局部变量“公会”

错误:“分配前引用了局部变量‘myData’”

Python 3:UnboundLocalError:分配前引用的局部变量[重复]

机器人框架:UnboundLocalError:分配前引用的局部变量“cellValue”