Introduction
Modern web applications often combine a powerful backend framework, a reliable relational database, and third-party APIs to provide dynamic content. In this tutorial, you'll build a Weather Application using Django, PostgreSQL, and the OpenWeatherMap API.
Instead of simply displaying weather data, the application will also store each city search in a PostgreSQL database, allowing you to view search history and extend the project with additional features later.
By the end of this guide, you'll learn how to:
- Install Django and PostgreSQL
- Configure PostgreSQL with Django
- Create a Django project and application
- Build database models
- Connect to the OpenWeatherMap API
- Display live weather information
- Save search history in PostgreSQL
- Run the project locally
- Prepare the project for production deployment
What You'll Build
The finished application includes:
- Weather search by city
- Live weather information
- Temperature
- Humidity
- Wind speed
- Weather icon
- Search history
- PostgreSQL database
- Responsive interface
Technology Stack
| Component | Technology |
|---|---|
| Backend | Django |
| Database | PostgreSQL |
| Database Tool | pgAdmin 4 |
| API | OpenWeatherMap |
| Frontend | HTML, CSS, Bootstrap |
| Language | Python |
Prerequisites
Install the following software before starting:
- Python 3.10+
- PostgreSQL
- pgAdmin 4
- Visual Studio Code
- Git (optional)
Image Placeholder: Hero banner showing Django + PostgreSQL + OpenWeatherMap architecture.
Step 1: Create the Project
mkdir weather_project
cd weather_project
python -m venv venv
# Windows
venv\Scripts\activate
# Linux/macOS
source venv/bin/activate
Install Django and PostgreSQL support:
pip install django psycopg2-binary requests python-dotenv
Image Placeholder: Terminal showing project creation and package installation.
Step 2: Create the Django Project
django-admin startproject config .
python manage.py startapp weather
Project structure:
weather_project/
│
├── config/
├── weather/
├── templates/
├── static/
├── manage.py
└── requirements.txt
Image Placeholder: VS Code Explorer with the generated project tree.
Step 3: Install PostgreSQL and pgAdmin
Install PostgreSQL and launch pgAdmin.
Create:
- Database:
weatherdb - User:
weatheruser - Password:
yourpassword
Image Placeholder: pgAdmin dashboard showing the new database.
Step 4: Configure Django to Use PostgreSQL
Update config/settings.py:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "weatherdb",
"USER": "weatheruser",
"PASSWORD": "yourpassword",
"HOST": "localhost",
"PORT": "5432",
}
}
Run migrations:
python manage.py migrate
Image Placeholder: Successful migration output in the terminal.
Step 5: Create the Weather Model
Create weather/models.py:
from django.db import models
class WeatherSearch(models.Model):
city = models.CharField(max_length=100)
temperature = models.FloatField()
description = models.CharField(max_length=200)
humidity = models.IntegerField()
wind_speed = models.FloatField()
searched_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.city
Create migrations:
python manage.py makemigrations
python manage.py migrate
Image Placeholder: Database table displayed in pgAdmin.
Step 6: Get an OpenWeatherMap API Key
- Register for a free OpenWeatherMap account.
- Generate an API key.
- Store the key in a
.envfile.
Example:
OPENWEATHER_API_KEY=YOUR_API_KEY
Image Placeholder: OpenWeatherMap API dashboard with the API key section highlighted.
Step 7: Create the Weather View
Implement a Django view that:
- Accepts a city name.
- Calls the OpenWeatherMap API.
- Parses the JSON response.
- Saves the result in PostgreSQL.
- Renders the weather details.
Image Placeholder: VS Code showing the view implementation.
Step 8: Build the User Interface
Create:
templates/weather/index.htmlstatic/css/style.css
The interface should include:
- Search form
- Weather card
- Weather icon
- Search history table
Image Placeholder: Browser displaying the completed weather dashboard.
Step 9: Configure URLs
Add application routes:
urlpatterns = [
path("", views.index, name="index"),
]
Image Placeholder: URL configuration highlighted in VS Code.
Step 10: Run the Application
Start the development server:
python manage.py runserver
Visit:
http://127.0.0.1:8000
Search for cities such as:
- London
- Paris
- Tokyo
- New York
The application retrieves the latest weather and stores the search in PostgreSQL.
Image Placeholder: Browser showing a successful weather search.
Step 11: Verify Data in PostgreSQL
Open pgAdmin.
Navigate to:
weatherdb
└── Schemas
└── public
└── weather_weathersearch
Run:
SELECT * FROM weather_weathersearch;
You should see all saved searches with timestamps and weather details.
Image Placeholder: pgAdmin query results displaying stored weather searches.
Step 12: Create a Search History Page
Display previous searches in a table:
| City | Temperature | Humidity | Date |
|---|
This demonstrates that the application is reading data from PostgreSQL.
Image Placeholder: Search history page in the browser.
Step 13: Project Architecture
Browser
│
▼
Django Views
│
├──────────────► OpenWeatherMap API
│ │
│ ▼
│ JSON Response
│
▼
PostgreSQL Database
│
▼
Templates (HTML/CSS)
│
▼
User Interface
Image Placeholder: Architecture diagram with icons for browser, Django, API, and PostgreSQL.
Step 14: Local Deployment
Collect static files:
python manage.py collectstatic
Apply migrations:
python manage.py migrate
Create an administrator account:
python manage.py createsuperuser
Start the application:
python manage.py runserver
Visit:
http://127.0.0.1:8000
The application is now running locally with PostgreSQL as its database.
Image Placeholder: Local deployment flow diagram.
Common Errors
psycopg2 Import Error
pip install psycopg2-binary
Database Connection Failed
- Check PostgreSQL is running.
- Verify credentials in
settings.py. - Confirm port
5432is open.
Invalid API Key
- Confirm the API key is correct.
- Wait a few minutes after generating a new key before first use.
Future Improvements
You can extend this project with:
- Five-day weather forecast
- User authentication
- Favorite cities
- Interactive weather maps
- Charts with historical data
- Docker support
- Deployment to Render, Railway, or AWS
- Celery for scheduled weather updates
- REST API using Django REST Framework
Conclusion
You have successfully built a full-stack Weather Application using Django, PostgreSQL, pgAdmin, and the OpenWeatherMap API. The project demonstrates how to integrate a third-party API, persist data in a relational database, and build a responsive web interface using Django's Model–View–Template architecture. It provides a strong foundation for expanding into more advanced features and production deployments.
Top comments (0)