欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用Django寫天氣預(yù)報(bào)查詢網(wǎng)站

 更新時(shí)間:2018年10月21日 14:11:49   作者:回憶不說話  
今天小編就為大家分享一篇關(guān)于用Django寫天氣預(yù)報(bào)查詢網(wǎng)站的文章,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧

創(chuàng)建項(xiàng)目

創(chuàng)建工程項(xiàng)目如下所示:

設(shè)置文件settings.py中的設(shè)置主要有兩個(gè)

1.注冊(cè)app

2.設(shè)置templates的路徑

前面的文章已經(jīng)介紹過多次如何設(shè)置了,此處不再做詳細(xì)贅述。

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

主要流程分以下幾步:

1.從接口獲取數(shù)據(jù),經(jīng)過urls.py文件傳送給index.html文件。

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

3.添加查詢功能。

獲取數(shù)據(jù)和傳送數(shù)據(jù)在前面的電影查詢網(wǎng)站已經(jīng)講過 ,這里著重說一下添加查詢功能的原理。

本次仍然是用form表單實(shí)現(xiàn)查詢功能,form表單的method不做設(shè)置的話會(huì)默認(rèn)get請(qǐng)求,當(dāng)我們第一次傳送數(shù)據(jù)到界面之后,

可以在form表單設(shè)置個(gè)請(qǐng)求方式,然后在下次請(qǐng)求數(shù)據(jù)的時(shí)候,添加一個(gè)判斷,判斷的時(shí)候把form表單輸入的城市信息更改

為下次請(qǐng)求的時(shí)候的城市信息即可。

下附代碼:

視圖文件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="請(qǐng)輸入城市">
    <button type="submit">查詢</button>
  </form>
  <main>
    <h2>實(shí)時(shí)天氣</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 啟動(dòng)服務(wù)器,在瀏覽器打開網(wǎng)址,即可看到效果:

 

在上面的查詢框中輸入城市名,即可查詢別的城市天氣信息:

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接

相關(guān)文章

最新評(píng)論