1. flask最簡單框架,返回hello world:from flask import *app = Flask(__name__)@app.route('/')def" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運營 > 如何用Python寫一個小網(wǎng)站?

如何用Python寫一個小網(wǎng)站?

時間:2024-01-21 15:30:01 | 來源:網(wǎng)站運營

時間:2024-01-21 15:30:01 來源:網(wǎng)站運營

如何用Python寫一個小網(wǎng)站?:

Flask 對初學(xué)者比較友好

下面教你使用 Python 的 flask 架構(gòu)搭建一個簡單web網(wǎng)站

1. flask最簡單框架,返回hello world:

from flask import *app = Flask(__name__)@app.route('/')def hello_world(): return 'Hello World!'if __name__=="__main__": app.run(host='0.0.0.0',port=8080) # 0.0.0.0地址可給本機所有的IP地址訪問

2. 讓flask可以給瀏覽器返回一個簡單的網(wǎng)頁,即添加視圖函數(shù),讓網(wǎng)頁變得豐富點:

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Wow</title></head><body> <h1>我超級帥喔!</h1></body></html>from flask import Flask,render_template,requestapp = Flask(__name__)@app.route('/')def hello_world(): return 'Hello World!' @app.route('/index')def index(): return render_template('index.html') #會自動找templates文件夾里面的index.html,并不需要一個絕對路徑。if __name__=="__main__": app.run(host='0.0.0.0',port=8080) # 0.0.0.0地址可給本機所有的IP地址訪問

3.同理我們可以讓 flask 添加更加多我們需要的web網(wǎng)頁:

from flask import Flask,render_template,requestapp = Flask(__name__)@app.route('/')def hello_world(): return 'Hello World!' @app.route('/index')def index(): return render_template('index.html') #會自動找templates文件夾里面的index.html,并不需要一個絕對路徑。@app.route('/login')def index(): return render_template('login.html') @app.route('/content')def index(): return render_template('content.html') if __name__=="__main__": app.run(host='0.0.0.0',port=8080) // login.html<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <form action="/login_finsh" method="POST"> 用戶名 <input type="text" name="username" id=""> 密碼 <input type="text" name="password" id=""> <input type="submit" value="登錄"> </br> <input type="button" value="open1"> </br> <button type="button" onclick="open_function()"> open1</button> </form></body></html>


// content.html<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <p>這是內(nèi)容頁!</p></body></html>

是不是很簡單?還不快動手敲一敲?



關(guān)鍵詞:

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關(guān)閉