用Django寫天氣預報查詢網站
創(chuàng)建項目
創(chuàng)建工程項目如下所示:

設置文件settings.py中的設置主要有兩個
1.注冊app
2.設置templates的路徑
前面的文章已經介紹過多次如何設置了,此處不再做詳細贅述。
主要流程分以下幾步:
1.從接口獲取數(shù)據(jù),經過urls.py文件傳送給index.html文件。
2.在index.html文件中做界面處理,使界面美觀好看。
3.添加查詢功能。
獲取數(shù)據(jù)和傳送數(shù)據(jù)在前面的電影查詢網站已經講過 ,這里著重說一下添加查詢功能的原理。
本次仍然是用form表單實現(xiàn)查詢功能,form表單的method不做設置的話會默認get請求,當我們第一次傳送數(shù)據(jù)到界面之后,
可以在form表單設置個請求方式,然后在下次請求數(shù)據(jù)的時候,添加一個判斷,判斷的時候把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博客的RSS功能的實現(xiàn)方法
- Django 博客實現(xiàn)簡單的全文搜索的示例代碼
- 基于Django統(tǒng)計博客文章閱讀量
- Python使用Django實現(xiàn)博客系統(tǒng)完整版
- 使用python和Django完成博客數(shù)據(jù)庫的遷移方法
- 利用Celery實現(xiàn)Django博客PV統(tǒng)計功能詳解
- Python采用Django開發(fā)自己的博客系統(tǒng)
- Python Django搭建網站流程圖解
- Django如何實現(xiàn)網站注冊用戶郵箱驗證功能
- python3.6+django2.0+mysql搭建網站過程詳解
- Python+Django搭建自己的blog網站
- 利用django創(chuàng)建一個簡易的博客網站的示例
相關文章
Python numpy實現(xiàn)二維數(shù)組和一維數(shù)組拼接的方法
今天小編就為大家分享一篇Python numpy實現(xiàn)二維數(shù)組和一維數(shù)組拼接的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
pycharm運行出現(xiàn)ImportError:No module named的解決方法
今天小編就為大家分享一篇pycharm運行出現(xiàn)ImportError:No module named的解決方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python超越函數(shù)積分運算以及繪圖實現(xiàn)代碼
今天小編就為大家分享一篇Python超越函數(shù)積分運算以及繪圖實現(xiàn)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11

