python-flask(1)(flask 快速开发)

flask文档

新建Flask程序

  • python3版本

  • 安装pip库

pip install flask

跨域问题处理

pip install flask_cors

from flask import Flask, jsonify, request, render_template

from flask_cors import *
​
app = Flask(__name__, template_folder="templates",
            static_folder="templates",
            static_url_path="")
CORS(app, supports_credentials=True)
@app.route('/')
def hello_world():
    return render_template("index.html")
​//   在这下面可以开始写路由了
if __name__ == '__main__':
    app.run(host="0.0.0.0", port=80)

文件结构

  • templates用来存放html 访问时可render_template("index.html")对应的html文件

文件结构

处理跨域

from flask_cors import *
CORS(app, supports_credentials=True)
文件
app = Flask(__name__, template_folder="templates",
            static_folder="templates",
            static_url_path="")

文件结构

  • template_folder : 指定存放模板的文件夹的名称(默认为templates)

  • .static_folder : 指定存放静态文件资源的文件夹的名称(默认为static)在html引入的css文件没有效果,可以加上这一句

  • 注意:如果没有指定static_url_path的话,则访问路径static_folder 的名称是一致的

  • .static_url_path : 指定静态文件的访问路径

运行

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=80)
requests,处理返回的json对象
@app.after_request
def add_header(response):
    response.cache_control.no_store = True
    return response

强制清除缓存(遇到缓存问题)

  • body = json.loads(request.get_data(as_text=True))
posted @ 2022-11-22 23:31  MyKai  阅读(63)  评论(0)    收藏  举报