0
votes

I m trying to make a form where user enter the state and weather of that location is given back

It was working fine untill I added cities = City.objects.all() in the code

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)

UnboundLocalError at / local variable 'form' referenced before assignment Request Method: GET Request URL: http://127.0.0.1:8000/ Django Version: 2.2.1 Exception Type: UnboundLocalError Exception Value: local variable 'form' referenced before assignment Exception Location: C:\Users\Admin\Desktop\the_weather\weathers\views.py in index, line 12 Python Executable: C:\Users\Admin\AppData\Local\Programs\Python\Python37-32\python.exe Python Version: 3.7.3 Python Path: ['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'] Server time: Fri, 24 May 2019 04:09:08 +0000

2

2 Answers

0
votes

You are making mistake here

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 is GET the it directly go to the form.save() before its assignment.

To solve this

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

form = CityForm()
0
votes

You need change:

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

to

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