Weather App Using Python Django

Introduction:
When a user enters the name of a city, the Weather App retrieves current weather information. It provides temperature, humidity, condition (such as clear or rainy), and wind speed by integrating Django with external APIs like OpenWeatherMap.
Users input the name of a city into a search bar. The weather API is contacted by the backend, which then parses the JSON response. Depending on the forecast, the outcome is shown with dynamic styling, weather icons, and extra advice.
The application renders weather cards using templates and manages logic using Django views. It can show the default cities or store search history. The challenge of rapidly checking the weather without visiting specialized weather websites is resolved by this project. It’s an excellent introduction to working with JSON data and Django APIs.
Its integration of real-time external data with Django’s backend is what makes it special. To improve the user experience, features like location-based recommendations, dark mode, and 5-day forecasts can be included.
Required Modules or Packages:
Python 3.8+: Make sure you have Python version 3.8 or higher installed on your system.
Django 3.x or 4.x: You’ll need the Django framework, version 3.x or 4.x, to build and run the system.
pip: This is the Python package manager that you’ll use to install the required libraries and packages.
You can install the core requirements with:
pip install django
pip install -r requirements.txt
How to Run the Code:
Follow these steps to launch the application locally:
✅ STEP 1: Download or Clone the Project
Option A: If you have a ZIP file
1. Extract it to a folder.
2. Open that folder in VS Code:
VS Code → File → Open Folder…
Option B: If using Git clone
git clone https://github.com/username/repository-name.git
cd repository-name
code .
✅ STEP 2: Set Up Virtual Environment
This keeps your project dependencies isolated.
# Create virtual environment (in project folder)
python -m venv venv
# Activate it:
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activate
✅ STEP 3: Install Requirements
Most Django projects have a requirements.txt file. Install the packages:
pip install -r requirements.txt
If requirements.txt is missing, you can manually install Django:
pip install django
✅ STEP 4: Configure .env or Settings (If Required)
Some projects require .env files (for secret keys or DB settings).
Check the README or .env.example if present, and create your own .env.
✅ STEP 5: Run Migrations
python manage.py makemigrations
python manage.py migrate
✅ STEP 6: Create Superuser (for Admin Login)
python manage.py createsuperuser
Follow the prompts.
✅ STEP 7: Run the Django Development Server
python manage.py runserver
Open http://127.0.0.1:8000 in your browser.
Use /admin to access the admin panel.
Code Explanation:
1. API Integration in views.py
def index(request):
if request.method == "POST":
city = request.POST['city']
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY'
response = requests.get(url).json()
This view takes the city name input from the user.
Sends a request to the OpenWeatherMap API using the city.
The returned JSON includes weather data.
2. Extracting Data from JSON
if response.get("cod") != "404":
data = {
"country_code": response['sys']['country'],
"coordinate": f"{response['coord']['lon']} {response['coord']['lat']}",
"temp": str(round(response['main']['temp'] - 273.15, 2)),
"pressure": response['main']['pressure'],
"humidity": response['main']['humidity']
}
else:
data = {}
Extracts values like temperature, pressure, humidity, and location.
Temperature is converted from Kelvin to Celsius.
If the city is not found, it returns an empty dictionary.
3. Rendering Data in index.html
<form method="POST">
{% csrf_token %}
<input type="text" name="city" placeholder="Enter City">
<button type="submit">Search</button>
</form>
{% if data %}
<div>
<h2>Weather for {{ city }}</h2>
<p>Country: {{ data.country_code }}</p>
<p>Coordinates: {{ data.coordinate }}</p>
<p>Temperature: {{ data.temp }} °C</p>
<p>Pressure: {{ data.pressure }} hPa</p>
<p>Humidity: {{ data.humidity }}%</p>
</div>
{% endif %}
Uses Django template syntax to dynamically display weather data.
Simple UI for input and result presentation.
🗺 URLs Routing (urls.py)
urlpatterns = [
path('', views.index, name='home'),
]
Routes the root path (/) to the weather query view.
Source Code:
To explore the full source code, click the button below to download the ZIP file and run it on your local machine:
Output:
