用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单线程实现多个定时器示例
Mar 30 Python
python使用PyGame模块播放声音的方法
May 20 Python
python利用OpenCV2实现人脸检测
Apr 16 Python
python将文本分每两行一组并保存到文件
Mar 19 Python
详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数
Apr 18 Python
django js实现部分页面刷新的示例代码
May 28 Python
对Python通过pypyodbc访问Access数据库的方法详解
Oct 27 Python
对pycharm 修改程序运行所需内存详解
Dec 03 Python
基于Django静态资源部署404的解决方法
Jul 28 Python
Python使用Socket实现简单聊天程序
Feb 28 Python
Tensorflow全局设置可见GPU编号操作
Jun 30 Python
Python 执行矩阵与线性代数运算
Aug 01 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
文件上传类
2006/10/09 PHP
PHP中的日期及时间
2006/11/23 PHP
PHP curl模拟浏览器采集阿里巴巴的实现代码
2011/04/20 PHP
PHP实现操作redis的封装类完整实例
2015/11/14 PHP
php接口技术实例详解
2016/12/07 PHP
PHP设置Cookie的HTTPONLY属性方法
2017/02/09 PHP
PHP环形链表实现方法示例
2017/09/15 PHP
js 字符串转换成数字的三种方法
2013/03/23 Javascript
jquery iframe操作详细解析
2013/11/20 Javascript
jQuery学习总结之jQuery事件
2014/06/30 Javascript
vue.js使用3DES加密的方法示例
2018/05/18 Javascript
vue v-model实现自定义样式多选与单选功能
2018/07/05 Javascript
详解vue axios用post提交的数据格式
2018/08/07 Javascript
微信小程序数据分析之自定义分析的实现
2018/08/17 Javascript
node错误处理与日志记录的实现
2018/12/24 Javascript
nodejs使用socket5进行代理请求的实现
2020/02/21 NodeJs
vuex的使用和简易实现
2021/01/07 Vue.js
MySQL适配器PyMySQL详解
2017/09/20 Python
Python 比较文本相似性的方法(difflib,Levenshtein)
2018/10/15 Python
selenium使用chrome浏览器测试(附chromedriver与chrome的对应关系表)
2018/11/29 Python
python学生管理系统
2019/01/30 Python
Python递归函数实例讲解
2019/02/27 Python
详解Python中namedtuple的使用
2020/04/27 Python
scrapy爬虫:scrapy.FormRequest中formdata参数详解
2020/04/30 Python
浅谈numpy中np.array()与np.asarray的区别以及.tolist
2020/06/03 Python
英国复古和经典球衣网站:Vintage Football Shirts
2018/10/05 全球购物
小型女装店的创业计划书
2014/01/09 职场文书
上班看电影检讨书
2014/02/12 职场文书
《春笋》教学反思
2014/04/15 职场文书
妇联主席先进事迹
2014/05/18 职场文书
总经理人事任命书
2014/06/05 职场文书
篮球比赛拉拉队口号
2014/06/10 职场文书
小学生放飞梦想演讲稿
2014/08/26 职场文书
荆州古城导游词
2015/02/06 职场文书
2015医德医风个人工作总结
2015/04/02 职场文书
初婚初育证明范本
2015/06/18 职场文书