当前位置: 首页 > news >正文

Weather app using Django - Python

我们的任务是使用 Django 创建一个 Weather 应用程序,让用户可以输入城市名称并查看当前天气详细信息,例如温度、湿度和压力。我们将通过设置一个 Django 项目,创建一个视图来从 OpenWeatherMap API 获取数据,并设计一个简单的模板来显示结果。我们将逐步实现核心功能来构建一个有效的天气应用程序。

前置条件

  1. Django的安装
  2. 创建新的新项目

基本安装

创建一个虚拟环境安装packages 

pip install Django

Step 1: 创建 Django 项目

启动一个名为 weather 的新 Django 项目:

django-admin startproject weather

Step 2: 导航到项目目录

cd weather

Step 3: 创建 Django 应用

创建一个名为 main 的应用程序:

python manage.py startapp main

目录结构应如下所示:

Step 4: 将主应用程序添加到设置

打开 weather/settings.py 并将 'main' 添加到INSTALLED_APPS列表中:

Step 5: Configure URLs

编辑 weather/urls.py 以包含待办事项应用程序视图:

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

Step 6: 定义主应用程序的 URL

创建 main/urls.py 并添加:

from django.urls import path
from . import viewsurlpatterns = [path('', views.index),
]

Step 7: Create the View

编辑 main/views.py 以获取和处理天气数据:注意:从 “OpenWeatherApp” 获取您的 API 密钥 将 views.py 中的“your_api_key_here”替换为您的实际 API 密钥。

Step 7: Create the Template

使用此内容创建 main/templates/main/index.html:

<!DOCTYPE html>
<html lang="en" dir="ltr"><head><meta charset="utf-8" /><title>weather</title><!-- Latest compiled and minified CSS --><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" /><!-- jQuery library --><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script><!-- Latest compiled JavaScript --><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
</head><body><nav class="row" style="background: green; color: white;"><h1 class="col-md-3 text-center">weather</h1></nav><br /><br /><center class="row"><form method="post" class="col-md-6 col-md-offset-3">{% csrf_token %}<div class="input-group"><input type="text" class="form-control" name="city" placeholder="Search" /><div class="input-group-btn"><button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button></div></div></form></center><div class="row">{% if country_code and coordinate and temp and pressure and humidity %}<div class="col-md-6 col-md-offset-3"><h3>Country Code: {{ country_code }}</h3><h5>Coordinate: {{ coordinate }}</h5><h5>Temperature: {{ temp }}</h5><h5>Pressure: {{ pressure }}</h5><h5>Humidity: {{ humidity }}</h5></div>{% endif %}</div>
</body></html>

Step 9: Make Migrations (Optional)

运行迁移以保持您的环境更新(即使此应用程序没有模型):

python manage.py makemigrations
python manage.py migrate

Step 10: Run the Server

启动 Django 开发服务器:

python manage.py runserver

Open your browser at "http://127.0.0.1:8000/", enter a city name, and check the weather!

http://www.xdnf.cn/news/687547.html

相关文章:

  • Denoising Autoencoders 视频截图 DAEs简单实现 kaggle 去噪编码器
  • 第二节 LED模块
  • 【普及−】洛谷P1706 全排列问题
  • java每日精进 5.28【幂等性】
  • 2025年05月28日Github流行趋势
  • uniapp-商城-74-shop(7-商品列表,选规格 添加商品到购物车)
  • 前端面试准备-1
  • Linux中的权限概念
  • Java SE Cloneable接口和深/浅拷贝
  • 水域应急救援可视化平台
  • 【前端】Vue3+elementui+ts,TypeScript Promise<string>转string错误解析,习惯性请出DeepSeek来解答
  • 国产SOC有哪些?
  • 即插即用的全新算法改进策略——引导学习策略:一种用于元启发式算法设计和改进的新型更新机制
  • Unity对象池插件Lean Pool学习笔记
  • android 图片背景毛玻璃效果实现
  • Flutter 与 Android 原生布局组件对照表(完整版)
  • TencentOSTiny
  • 【模型显著性分析】配对样本 t 检验
  • 虚拟与现实时空认知同步的核心指标
  • maven中的maven-resources-plugin插件详解
  • 部署LVS-DR群集
  • Docker部署Spark大数据组件:配置log4j日志
  • Vue开发系列——零基础HTML引入 Vue.js 实现页面之间传参
  • Kotlin 中的数据类型有隐式转换吗?为什么?
  • 天津工作机会:技术文档工程师 - 华海清科股份有限公司
  • 【Linux】分页式存储管理:深刻理解页表映射
  • 【Doris基础】Apache Doris 基本架构深度解析:从存储到查询的完整技术演进
  • 金砖国家人工智能高级别论坛在巴西召开,华院计算应邀出席并发表主题演讲
  • 960g轻薄本,把科技塞进巧克力盒子
  • 从零开始学安全:服务器被入侵后的自救指南