Weather App in Djnago

django-admin startproject WeatherApp
python manage.py startapp example
In the installed app list add the app name
In DIRS add ‘templates’ and create a templates folder in the root directory
from django.contrib import adminfrom django.urls import path,includeurlpatterns = [path('admin/', admin.site.urls),path('',include('example.urls'))]
from django.urls import path,includefrom . import viewsurlpatterns = [path('',views.getdata,name="getdata")]
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)
from django.contrib import adminfrom .models import Weather# Register your models here.admin.site.register(Weather)
from django.shortcuts import renderimport requestsfrom .models import Weather# Create your views here.def getdata(request):base_url = "http://api.openweathermap.org/data/2.5/weather?"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')
python manage.py makemigrations
python manage.py migrate
<!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="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" 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{{ p.place }} and Temperature is {{ p.description }}</div>{% endfor %}<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/js/bootstrap.bundle.min.js" integrity="sha384-JEW9xMcG8R+pH31jmWH6WWP0WintQrMb4s7ZOdauHnUtxwoG2vI5DkLtS3qm9Ekf" crossorigin="anonymous"></script></body></html>
You can override __str__ method inside the model class to get more readable form of the objects
def __str__(self):return self.description
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
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.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Never Give Up, Retry: How Software Should Deal with Failures

Publishing Merged Code Coverage Report of Nx Workspace in Azure CI Pipeline

It’s Alive! Drupal 7 gets another year

The everliving Drupal 7

Kubernetes Integration with Python-CGI

[ Java Testing ] Validating Private Fields and Methods using Java Reflections

Python and its applications explained in less than 800 words

python-applications-blog-angry-ventures-photo-by-chris-ried

How to avoid the biggest newbie programmer mistakes — WeAreBrain Blog

What is Variable in Python?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nilay Paul

Nilay Paul

More from Medium

How to store images on MongoDB

QR code generator API with Django

Deploying a Flask App to Heroku with MongoDB using QuotaGuard

How to use Google Calendar APIs with NodeJs with Oauth 2.0 🎉