Giter Club home page Giter Club logo

weather's Introduction

in the terminal

Create a virtual environment and install Django

python3 -m venv venv

#if you are using mac use next command as : source venv/bin/activate

REMEMBER if you are using windows

venv\Scripts\activate

pip install django

Create a Django project

django-admin startproject weather

cd into the weather directory

cd weather_app

Create the weather app

python manage.py startapp weather

Add the weather app to the installed apps in settings.py

INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'weather', 'weather_app', ]

Add the OpenWeatherMap API key to settings.py

OPENWEATHERMAP_API_KEY = 'YOUR_API_KEY_HERE' Create the necessary views in the weather/views.py file: from django.shortcuts import render from django.http import request,HttpResponse import requests

Create your views here.

def home(request): if request.method == 'POST': city = request.POST.get('city') url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid=YOUR_API_KEY' response = requests.get(url) data = dict(response.json()) return render(request,'weather.html',{ 'city':data['name'], 'main':data['weather'][0]['main'], 'temp': data['main']['temp'], 'max':data['main']['temp_max'], 'min':data['main']['temp_min'], 'feels':data['main']['feels_like'], }) return render(request,'index.html',{})

<title>Weather App</title>



    {% csrf_token %}
    <label for="city">Your City Name</label>
<title>Weather at finger tips</title>


<h1>{{city}}</h1>
<h3>Today we have some {{main}} in weahter</h3>
<h3>Temprature: {{temp}} <br>Feels like: {{feels}}</h3>
#Update the urls.py file in the project's root directory:

from django.urls import path from app import views

urlpatterns = [ path('',views.home), ]

Make migrations for the weather app

python manage.py makemigrations weather

Migrate the changes to the database

python manage.py migrate

Now in the terminal again

Run the server

python manage.py runserver

Open the app in a web browser

http://localhost:8000

weather's People

Contributors

mj6725 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.