網(wǎng)站首頁 編程語言 正文
在本教程中,我們將學(xué)習(xí)如何創(chuàng)建一個使用Django作為后端的天氣應(yīng)用程序。Django提供了一個基于Python Web框架的Web框架,允許快速開發(fā)和干凈,務(wù)實的設(shè)計。
基本設(shè)置
將目錄更改為天氣
cd weather
啟動服務(wù)器
python manage.py runserver
要檢查服務(wù)器是否正在運(yùn)行,請轉(zhuǎn)到 Web 瀏覽器并輸入為 URL。現(xiàn)在,您可以通過按以下命令停止服務(wù)器http://127.0.0.1:8000/
ctrl-c
實現(xiàn)
python manage.py startapp main
轉(zhuǎn)到主/文件夾通過做:
cd main
并創(chuàng)建一個包含文件的文件夾:templates/main/index.html
index.html
使用文本編輯器打開項目文件夾。目錄結(jié)構(gòu)應(yīng)如下所示:
現(xiàn)在添加主應(yīng)用settings.py
在天氣中編輯 urls.py?文件:
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('main.urls')), ]
在主文件中編輯 urls.py?文件:
from django.urls import path from . import views urlpatterns = [ path('', views.index), ]
在`主文件中編輯 views.py :
from django.shortcuts import render # 導(dǎo)入json以將json數(shù)據(jù)加載到python字典 import json # urllib.request 向api發(fā)出請求 import urllib.request def index(request): if request.method == 'POST': city = request.POST['city'] ''' api密鑰可能已過期,請使用您自己的api密鑰 將api_key替換為appid=“your_api_key_here” ''' # 包含來自API的JSON數(shù)據(jù) source = urllib.request.urlopen( 'http://api.openweathermap.org/data/2.5/weather?q =' + city + '&appid = your_api_key_here').read() # 將JSON數(shù)據(jù)轉(zhuǎn)換為字典 list_of_data = json.loads(source) # 變量list_of_data的數(shù)據(jù) data = { "country_code": str(list_of_data['sys']['country']), "coordinate": str(list_of_data['coord']['lon']) + ' ' + str(list_of_data['coord']['lat']), "temp": str(list_of_data['main']['temp']) + 'k', "pressure": str(list_of_data['main']['pressure']), "humidity": str(list_of_data['main']['humidity']), } print(data) else: data ={} return render(request, "main/index.html", data)
您可以從中獲取自己的 API 密鑰:?天氣 API
導(dǎo)航并編輯它:鏈接到索引.html文件templates/main/index.html
進(jìn)行遷移并遷移:
python manage.py makemigrations python manage.py migrate
現(xiàn)在,讓我們運(yùn)行服務(wù)器以查看天氣應(yīng)用。
python manage.py runserver
原文鏈接:https://juejin.cn/post/7157347431863025672
相關(guān)推薦
- 2022-08-02 Python?Pandas處理CSV文件的常用技巧分享_python
- 2022-05-08 Python進(jìn)程間的通信一起來了解下_python
- 2022-11-07 python多進(jìn)程使用apply_async的使用方法詳解_python
- 2022-09-18 go?mock?server的簡易實現(xiàn)示例_Golang
- 2022-07-24 Python寫安全小工具之TCP全連接端口掃描器_python
- 2022-04-16 pycharm如何設(shè)置自動生成作者信息_python
- 2022-06-20 Python使用MapReduce編程模型統(tǒng)計銷量_python
- 2022-08-16 C#工程建立后修改工程文件名與命名空間操作_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支