예제 #1
0
파일: views.py 프로젝트: es3573/Lab6
def index(request):
    src = ''
    dst = ''
    weather_url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=33815ab6524fca76a8bbe9a95df35754'
    
    cities = City.objects.all()        

    if request.method == 'POST':
        form = CityForm(request.POST)
        form.save()
    else:
        form = CityForm()
        
    weather_data = []

    for city in cities:
        city_weather = requests.get(weather_url.format(city)).json()
        
        weather = {
                'city' : city,
                'temperature' : city_weather['main']['temp'],
                'description' : city_weather['weather'][0]['description'],
                'icon' : city_weather['weather'][0]['icon']
        }
        weather_data.append(weather)
        city.temp = weather['temperature']
        if city.option == '1':
            src = str(city.name)
        elif city.option == '2':
            dst = str(city.name)
            
        city.save()

    context = {'weather_data' : weather_data, 'form' : form, 'src' : src, 'dst' : dst}
    return render(request, 'weather/index.html', context)
예제 #2
0
def city_new(request):
    if request.method == "POST":
        form = CityForm(request.POST, request.FILES)
        if form.is_valid():
            city = form.save(commit=False)
            city.save()
            form.save_m2m()
            return redirect('website.views.city_detail', pk=city.pk)
    else:
        form = CityForm()
    return render(request, 'website/city_new.html', {'form': form})
예제 #3
0
def city_edit(request, pk):
    city = get_object_or_404(City, pk=pk)
    if request.method == "POST":
        form = CityForm(request.POST, request.FILES, instance=city)
        if form.is_valid():
            city = form.save(commit=False)
            city.save()
            form.save_m2m()
            return redirect('website.views.city_detail', pk=city.pk)
    else:
        form = CityForm(instance=city)
    return render(request, 'website/city_new.html', {'form': form})
예제 #4
0
def index(request):
    url = '''http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=fd1ddc9ac4c6b84d80efc1908d3d0944'''
    cities = City.objects.all()  #return all the cities in the database

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

        if not City.objects.filter(name=form['name'].value()).exists() and (
                requests.get(url.format(form['name'].value())).status_code
                == 200):  #only saves if city is valid
            #and does not exist
            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': round((city_weather['main']['temp'] - 32) * 5 / 9,
                                 1),  #covert Fahrenheit to Celsius
            '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, 'weather/index.html',
                  context)  #return index.html template