用Django写天气预报查询网站


Posted in Python onOctober 21, 2018

创建项目

创建工程项目如下所示:

用Django写天气预报查询网站

设置文件settings.py中的设置主要有两个

1.注册app

2.设置templates的路径

前面的文章已经介绍过多次如何设置了,此处不再做详细赘述。

接口api为:http://api.map.baidu.com/telematics/v3/weather?location=%E9%83%91%E5%B7%9E&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?

主要流程分以下几步:

1.从接口获取数据,经过urls.py文件传送给index.html文件。

2.在index.html文件中做界面处理,使界面美观好看。

3.添加查询功能。

获取数据和传送数据在前面的电影查询网站已经讲过 ,这里着重说一下添加查询功能的原理。

本次仍然是用form表单实现查询功能,form表单的method不做设置的话会默认get请求,当我们第一次传送数据到界面之后,

可以在form表单设置个请求方式,然后在下次请求数据的时候,添加一个判断,判断的时候把form表单输入的城市信息更改

为下次请求的时候的城市信息即可。

下附代码:

视图文件views.py文件中的代码如下:

from django.shortcuts import render
import requests
# Create your views here.
def index(request):
  if request.method == 'POST':
    city = request.POST['city']
    url = 'http://api.map.baidu.com/telematics/v3/weather?location={}&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'.format(city)
  else:
    url = 'http://api.map.baidu.com/telematics/v3/weather?location=%E9%83%91%E5%B7%9E&output=json&ak=TueGDhCvwI6fOrQnLM0qmXxY9N0OkOiQ&callback=?'
  json_data = requests.get(url).json()
  weather = json_data['results'][0]['weather_data']
  today_weather = weather[0]
  t_weather = weather[1]
  tt_weather = weather[2]
  ttt_weather =weather[3]
  city = json_data['results'][0]['currentCity']
  context = {
    'today':today_weather,
    'city':city,
    'list':[t_weather,tt_weather,ttt_weather]
  }
  return render(request,'index.html',context)

urls.py文件中的代码如下:

from django.contrib import admin
from django.urls import path
from myApp import views
urlpatterns = [
  path('admin/', admin.site.urls),
  path('index/',views.index),
  path('select/',views.index),
]

index.html界面文件代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>{{city}}天气信息</title>
  <style>
    html,body{
      height:100%;
      margin:0;
      color: white;
      text-align: center;
    }
    body{
      /*渐变色*/
      background: linear-gradient(#1078c7,#7196b4);
    }
    form{
      text-align: center;
    }
    main img{
      width: 80px;
    }
    h1{
      margin:5px;
    }
    footer{
      display: flex;
    }
    section{
      flex-grow: 1;
      border-right:1px solid greenyellow;
    }
    section:nth-child(3){
      border:none;
    }
  </style>
</head>
<body>
  <form action="/select/" method="POST">
    {% csrf_token %}
    <input name="city" type="text" placeholder="请输入城市">
    <button type="submit">查询</button>
  </form>
  <main>
    <h2>实时天气</h2>
    <img src="{{today.dayPictureUrl}}" alt="">
    <h1>{{today.temperature}}</h1>
    <div>
      {{today.weather}}<br>
      {{today.wind}}<br>
      {{today.date}}<br>
    </div>
  </main>
  <footer>
    {% for weather in list %}
      <section>
        <h4>{{weather.date}}</h4>
        <img src="{{weather.dayPictureUrl}}" alt="">
        <div>
          {{weather.temperature}}<br>
          {{weather.weather}}<br>
          {{weather.wind}}<br>
        </div>
      </section>
    {% endfor %}
  </footer>
</body>
</html>

python manage.py runserver 启动服务器,在浏览器打开网址,即可看到效果:

 用Django写天气预报查询网站

在上面的查询框中输入城市名,即可查询别的城市天气信息:

用Django写天气预报查询网站

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。如果你想了解更多相关内容请查看下面相关链接

Python 相关文章推荐
剖析Python的Twisted框架的核心特性
May 25 Python
python 查找字符串是否存在实例详解
Jan 20 Python
浅谈Python实现Apriori算法介绍
Dec 20 Python
python版本的仿windows计划任务工具
Apr 30 Python
Python用csv写入文件_消除空余行的方法
Jul 06 Python
Python3利用Dlib实现摄像头实时人脸检测和平铺显示示例
Feb 21 Python
一步步教你用python的scrapy编写一个爬虫
Apr 17 Python
使用Windows批处理和WMI设置Python的环境变量方法
Aug 14 Python
使用python将最新的测试报告以附件的形式发到指定邮箱
Sep 20 Python
Python实现栈的方法详解【基于数组和单链表两种方法】
Feb 22 Python
Python GUI编程学习笔记之tkinter中messagebox、filedialog控件用法详解
Mar 30 Python
Python如何进行时间处理
Aug 06 Python
Django中数据库的数据关系:一对一,一对多,多对多
Oct 21 #Python
python高效过滤出文件夹下指定文件名结尾的文件实例
Oct 21 #Python
Python根据文件名批量转移图片的方法
Oct 21 #Python
浅谈Python中的bs4基础
Oct 21 #Python
python清除字符串前后空格函数的方法
Oct 21 #Python
Windows系统下PhantomJS的安装和基本用法
Oct 21 #Python
Scrapy框架使用的基本知识
Oct 21 #Python
You might like
【星际争霸1】人族1v7家ZBath
2020/03/04 星际争霸
PHP number_format() 函数定义和用法
2012/06/01 PHP
PHP中的类型提示(type hinting)功能介绍
2015/07/01 PHP
joomla数据库操作示例代码
2016/01/06 PHP
PHP MVC框架路由学习笔记
2016/03/02 PHP
php smtp实现发送邮件功能
2017/06/22 PHP
JavaScript中解析JSON数据的三种方法
2015/07/03 Javascript
酷! 不同风格页面布局幻灯片特效js实现
2021/02/19 Javascript
javascript判断元素存在和判断元素存在于实时的dom中的方法
2017/01/17 Javascript
NodeJs下的测试框架Mocha的简单介绍
2017/02/22 NodeJs
详解React-Native解决键盘遮挡问题(Keyboard遮挡问题)
2017/07/13 Javascript
express默认日志组件morgan的方法
2018/04/05 Javascript
webpack 3.X学习之多页面打包的方法
2018/09/04 Javascript
vue中进行微博分享的实例讲解
2019/10/14 Javascript
JS removeAttribute()方法实现删除元素的某个属性
2021/01/11 Javascript
深入理解Python 代码优化详解
2014/10/27 Python
python版简单工厂模式
2017/10/16 Python
Python批量查询关键词微信指数实例方法
2019/06/27 Python
浅析使用Python搭建http服务器
2019/10/27 Python
python给图像加上mask,并提取mask区域实例
2020/01/19 Python
Python进阶之迭代器与迭代器切片教程
2020/01/29 Python
PyInstaller将Python文件打包为exe后如何反编译(破解源码)以及防止反编译
2020/04/15 Python
python 密码学示例——凯撒密码的实现
2020/09/21 Python
利用Python将多张图片合成视频的实现
2020/11/23 Python
使用Python判断一个文件是否被占用的方法教程
2020/12/16 Python
普师专业个人自荐信范文
2013/11/26 职场文书
出生医学证明样本
2014/01/17 职场文书
董事长助理岗位职责
2014/02/18 职场文书
审计局2014法制宣传日活动总结
2014/11/01 职场文书
小学班主任事迹材料
2014/12/17 职场文书
2015年语文教学工作总结
2015/05/25 职场文书
公司酒会主持词
2015/07/02 职场文书
党员发展大会主持词
2015/07/03 职场文书
超市员工管理制度
2015/08/06 职场文书
运输公司工作总结
2015/08/11 职场文书
简单谈谈Python面向对象的相关知识
2021/06/28 Python