0% found this document useful (0 votes)
12 views3 pages

WeatherAPI Integration

This document outlines the steps to create a Django application that fetches weather data from the OpenWeatherMap API. It includes instructions for setting up a virtual environment, installing necessary packages, creating a project and app, and implementing views and templates for displaying weather information. Additionally, it provides code snippets for integrating the API and rendering the weather data in a user-friendly format.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

WeatherAPI Integration

This document outlines the steps to create a Django application that fetches weather data from the OpenWeatherMap API. It includes instructions for setting up a virtual environment, installing necessary packages, creating a project and app, and implementing views and templates for displaying weather information. Additionally, it provides code snippets for integrating the API and rendering the weather data in a user-friendly format.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

• Create virtual environment

• Activate environment
• Install Django & requests
• Create project
• Create app
• Open [Link] and sign up
• Generate API Key
• Register your app in [Link] file
• Add this to your [Link]
o OPENWEATHERMAP_API_KEY = 'YOUR_API_KEY_HERE'
• Create weather_service.py in your myapp folder and add below code

import requests
from [Link] import settings

def get_weather_data(city):
api_key = settings.OPENWEATHERMAP_API_KEY
url =
f"[Link]
_key}"

try:
response = [Link](url)
return [Link]()
except Exception as e:
print(f"Error fetching weather: {e}")
return None

• In [Link]

from [Link] import render


from weatherapp.weather_service import get_weather_data

def weather_view(request):
weather_data = None

if [Link] == 'POST':
city = [Link]('city', '')
weather_data = get_weather_data(city)

return render(request, '[Link]', {'weather': weather_data})


• Create [Link] in templates and add below code

<!DOCTYPE html>
<html>
<head>
<title>Weather Dashboard</title>
<style>
body { font-family: Arial; max-width: 600px; margin: auto; }
.weather-card {
border: 1px solid #ddd;
padding: 20px;
margin-top: 20px;
}
</style>
</head>
<body>
<h1>Weather Dashboard</h1>

<form method="post">
{% csrf_token %}
<input type="text" name="city" placeholder="Enter city name" required>
<button type="submit">Get Weather</button>
</form>

{% if weather %}
<div class="weather-card">
<h2>{{ [Link] }}, {{ [Link] }}</h2>
<p>Temperature: {{ [Link] }}°C</p>
<p>Feels Like: {{ [Link].feels_like }}°C</p>
<p>Description: {{ [Link] }}</p>
</div>
{% endif %}
</body>
</html>

• Create [Link] in myapp and add below code

from [Link] import path


from [Link] import weather_view

urlpatterns = [
path('', weather_view, name='weather'),
]

• Run your code

You might also like