Weather App in Djnago

First let’s have the api_key .

Click here to go to the website of weather api. Create an account and get the api key .

Weather api provides different services but for out project we will be using ->

Now Assuming you have the api key ….

Create a project

django-admin startproject WeatherApp

Enter into the WeatherApp folder and run->

python startapp example

Final folder structure

Register the app in the project .In of the “weather” folder

In the installed app list add the app name
In DIRS add ‘templates’ and create a templates folder in the root directory

In the project level file add

from django.contrib import adminfrom django.urls import path,includeurlpatterns = [path('admin/',,path('',include('example.urls'))]

In the app folder create file and add

from django.urls import path,includefrom . import viewsurlpatterns = [path('',views.getdata,name="getdata")]

In create a new model ->

from django.db import models# Create your models here.class Weather(models.Model):place = models.CharField(max_length=250)description=models.CharField(max_length=250)

Now we have to register the model in

from django.contrib import adminfrom .models import Weather# Register your models

In the app level add (Make sure to indent the code)

from django.shortcuts import renderimport requestsfrom .models import Weather# Create your views here.def getdata(request):base_url = ""if(request.method=="POST"):city_name=request.POST['city_name']API_KEY="0f511e87f0d6cd2622d48ef8805fbd46"URL=base_url+"q="+city_name+"&appid="+API_KEYdata = requests.get(URL).json()description = data['weather'][0]['description']temp = data['main']['temp']feels_like = data['main']['feels_like']humidity = data['main']['humidity']place = data['name']weather = Weather.objects.create(place=place,description=description)previous_search = Weather.objects.all()context={'description':description,'temp':temp,'feels_like':feels_like,'humidity':humidity,'place':place,'prev':previous_search}print(previous_search)print(data)return render(request,'index.html',context)return render(request,'index.html')

Run the following commands to make required database tables

python makemigrations
python migrate

Make a .html file inside the templates folder

Name it index.html and add

<!doctype html><html lang="en"><head><!-- Required meta tags --><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><!-- Bootstrap CSS --><link href="" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous"><title>Weather</title></head><body><form action="/" method="post">{% csrf_token %}<div class="input-group"><span class="input-group-text">Enter city name</span><input type="text" name="city_name"  class="form-control"></div></form><div class="card"><div class="card-body" style="text-align: center;">You have searched for {{ place }}</div></div><div class="card"><div class="card-body">Description of the weather : {{ description }}</div></div><div class="card"><div class="card-body">Temp in kelvin : {{ temp }} but feels like {{feels_like}}</div></div><div class="card"><div class="card-body">Humidity : {{ humidity }}</div></div><h3>Previous searched places</h3>{% for p in prev %}<div class="card-body">City{{ }} and Temperature is {{ p.description }}</div>{% endfor %}<script src="" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script></body></html>

Run “python createsuperuser” to create an admin . After successful creation run “python runserver” to spin up the server

open the link in a browser and go to “” . You will see something like this

After login you will see the search history of this site

You can override __str__ method inside the model class to get more readable form of the objects
def __str__(self):return self.description

In that case your will look like

from django.db import models# Create your models here.class Weather(models.Model):place = models.CharField(max_length=250)description=models.CharField(max_length=250)def __str__(self):return self.description

The final result

Now I am leaving the design up to you add some css if you want


  1. We can interact with databases without writing SQL instead we can use python classes.
  2. We can dynamically send data to html with “Jinja” .
  3. Built-in robust admin panel.
  4. csrf_token for added security.
  5. Django comes batteries included.