【问题标题】:Django local variable 'form' referenced before assignment分配前引用的 Django 局部变量“表单”
【发布时间】: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

【问题讨论】:

    标签: django


    【解决方案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()
      

      【讨论】:

        猜你喜欢
        • 2019-05-01
        • 1970-01-01
        • 2021-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多