python3 -m venv venv
#if you are using mac use next command as : source venv/bin/activate
venv\Scripts\activate
pip install django
django-admin startproject weather
cd weather_app
python manage.py startapp weather
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'weather', 'weather_app', ]
OPENWEATHERMAP_API_KEY = 'YOUR_API_KEY_HERE' Create the necessary views in the weather/views.py file: from django.shortcuts import render from django.http import request,HttpResponse import requests
def home(request): if request.method == 'POST': city = request.POST.get('city') url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY' response = requests.get(url) data = dict(response.json()) return render(request,'weather.html',{ 'city':data['name'], 'main':data['weather'][0]['main'], 'temp': data['main']['temp'], 'max':data['main']['temp_max'], 'min':data['main']['temp_min'], 'feels':data['main']['feels_like'], }) return render(request,'index.html',{})
<title>Weather App</title>
{% csrf_token %}
<label for="city">Your City Name</label>
<title>Weather at finger tips</title>
<h1>{{city}}</h1>
<h3>Today we have some {{main}} in weahter</h3>
<h3>Temprature: {{temp}} <br>Feels like: {{feels}}</h3>
#Update the urls.py file in the project's root directory:
from django.urls import path from app import views
urlpatterns = [ path('',views.home), ]
python manage.py makemigrations weather
python manage.py migrate
python manage.py runserver