赞
踩
pip install flask
from flask import Flask,request
app = Flask(__name__)
@app.route('/',methods = ['POST', 'GET'])
def hello_world():
if request.method == 'POST':
user = request.form['user']
print(user)
if request.method == 'GET':
user = request.args.get('user')
print(user)
return "hello_world!" # 给前端返回文本类型
if __name__ == '__main__':
app.run(host="127.0.0.1",port=8080,debug=True)
return "hello_world!"
import json
return json.dumps({'success':'1','msg':'欢迎访问!'})
from flask import Flask,request,redirect,url_for app = Flask(__name__) import json @app.route('/home',methods = ['POST', 'GET']) def hello_world(): if request.method == 'GET': user = request.args.get('user') print(user) return json.dumps({'success':'1','msg':'欢迎访问!'}) @app.route('/') def abc(): return redirect(url_for('hello_world',user="123")) # 重定向到/home页面 if __name__ == '__main__': app.run(host="127.0.0.1",port=8080,debug=False)
from flask import Flask,render_template
app = Flask(__name__)
@app.route('/',methods = ['POST', 'GET'])
def hello_world():
data_list = {'msg':'欢迎光临'}
return render_template('html/Home.html',data_list=data_list)
if __name__ == '__main__':
app.run(host="127.0.0.1",port=8080,debug=True)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="/static/js/vue.js"></script>
<link rel="stylesheet" type="text/css" href="/static/css/bod.css">
<title>Title</title>
</head>
<body>
<img src="/static/photo/lze.png">
{{ data_list }}
</body>
</html>
步骤:py文件同级建立templates和static文件夹,templates文件夹放html文件,static文件夹放js、css、图片等静态文件
(一)py文件
from flask import Flask,render_template
app = Flask(__name__,static_url_path='/abc',static_folder="abc",template_folder='abc/html')
@app.route('/',methods = ['POST', 'GET'])
def hello_world():
data_list = {'msg':'欢迎光临'}
return render_template('Home.html',data_list=data_list)
if __name__ == '__main__':
app.run(host="127.0.0.1",port=8080,debug=True)
(二)html写法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="/abc/js/vue.js"></script>
<link rel="stylesheet" type="text/css" href="/abc/css/bod.css">
<title>Title</title>
</head>
<body>
<img src="/abc/photo/lze.png">
{{ data_list }}
</body>
</html>
(三)静态目录结构
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。