NameError:名称“ CityForm”未定义

时间:2019-05-24 16:46:20

标签: django

我试图获取用户输入的城市的温度

from django.shortcuts import render
import requests
from  .models import City

def index(request):
        cities = City.objects.all() #return all the cities in the database

        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)
Traceback (most recent call last):
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Admin\Desktop\the_weather\weathers\views.py", line 14, in index
form = CityForm()
NameError: name 'CityForm' is not defined
[24/May/2019 10:35:31] "GET / HTTP/1.1" 500 64667

2 个答案:

答案 0 :(得分:0)

您需要从定义的任何地方(可能是在forms.py中)导入CityForm

答案 1 :(得分:0)

import requests
from django.shortcuts import render
from  .models import City
from .forms import CityForm

def index(request):
    url = 'http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=6169d48251197975732f77247af50079'
    if request.method == 'POST':
        form = CityForm(request.POST)
        form.save()

    form = CityForm()

    cities = City.objects.all()

    weather_data = []

    for city in cities:

        r = requests.get(url.format(city)).json()

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

        weather_data.append(city_weather)

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