Creating a weather app in Django involves setting up a Django project, integrating a weather API to fetch weather data, and displaying this data through a web interface. Here’s a step-by-step guide to building a basic weather app:
1. Set Up Your Django Project
First, install Django if you haven’t already:
pip install django
Create a new Django project and app:
django-admin startproject weather_project
cd weather_project
python manage.py startapp weather
2. Configure Your Django Project
Add your new app to the project’s settings. Open weather_project/settings.py
and add 'weather'
to the INSTALLED_APPS
list:
INSTALLED_APPS = [
...
'weather',
]
3. Create Your Weather App Models
For this simple app, we don’t need complex models since we’ll be fetching data from an external API. However, you can define models if needed for storing user data or settings. Skip to the next step if not needed.
4. Integrate a Weather API
Choose a weather API (e.g., OpenWeatherMap, Weatherbit). Sign up and get an API key. For this example, we’ll use OpenWeatherMap. Install the requests
library if you haven’t already:
pip install requests
Create a file weather/views.py
to handle fetching and displaying weather data:
import requests
from django.shortcuts import render
def get_weather(city):
api_key = 'your_openweathermap_api_key'
base_url = 'http://api.openweathermap.org/data/2.5/weather'
params = {
'q': city,
'appid': api_key,
'units': 'metric'
}
response = requests.get(base_url, params=params)
return response.json()
def weather_view(request):
city = request.GET.get('city', 'London') # Default to London
weather_data = get_weather(city)
return render(request, 'weather/weather.html', {'weather_data': weather_data, 'city': city})
5. Create Templates
Create a directory weather/templates/weather/
and add a file weather.html
:
<!DOCTYPE html>
<html>
<head>
<title>Weather App</title>
</head>
<body>
<h1>Weather in {{ city }}</h1>
{% if weather_data.cod == 200 %}
<p>Temperature: {{ weather_data.main.temp }}°C</p>
<p>Weather: {{ weather_data.weather.0.description }}</p>
<p>Humidity: {{ weather_data.main.humidity }}%</p>
{% else %}
<p>Weather data not available for {{ city }}</p>
{% endif %}
<form method="get">
<input type="text" name="city" placeholder="Enter city" value="{{ city }}">
<button type="submit">Get Weather</button>
</form>
</body>
</html>
6. Configure URLs
Add a URL pattern to handle requests. In weather/urls.py
:
from django.urls import path
from . import views
urlpatterns = [
path('', views.weather_view, name='weather_view'),
]
Include these URLs in the main project’s URL configuration. In weather_project/urls.py
:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('weather.urls')),
]
7. Run Your Django App
Finally, run the Django development server:
python manage.py runserver
Visit http://localhost:8000/
in your web browser. You should see a form to enter a city and display the weather data.
8. Add Error Handling (Optional)
Consider adding error handling for API failures or invalid inputs to improve the user experience.
This basic weather app demonstrates how to integrate a third-party API into a Django project and display the data. You can expand this app by adding more features, such as forecasts, user accounts, or improved UI/UX.