初始文档
This commit is contained in:
102
开发文档/python/后端/Flask用法简单示例.md
Normal file
102
开发文档/python/后端/Flask用法简单示例.md
Normal file
@@ -0,0 +1,102 @@
|
||||
# 示例app.py文件
|
||||
```shell
|
||||
# 需要安装的库
|
||||
pip install flask flask_cors ulid flask_sslify
|
||||
```
|
||||
```python
|
||||
# 项目根目录文件包含:templates,static
|
||||
# templates文件包含模板页.static包含静态文件,如:js,css文件,图片视频等.
|
||||
# 文件名app.py
|
||||
# 文件路径:/app.py
|
||||
# 运行环境python3.7以上版本
|
||||
|
||||
from flask import Flask, render_template, request, url_for, make_response
|
||||
from flask_cors import *
|
||||
from OpenSSL import SSL
|
||||
from flask_sslify import SSLify
|
||||
import json
|
||||
|
||||
# 创建flask应用
|
||||
app = Flask(__name__)
|
||||
# 解决跨域问题
|
||||
CORS(app, supports_credentials=True)
|
||||
# 使用https
|
||||
sslify=SSLify(app)
|
||||
# 测试数据
|
||||
data={
|
||||
'appid':'123456',
|
||||
'secret':'123456',
|
||||
'js_code':'123456',
|
||||
'grant_type':'authorization_code'
|
||||
}
|
||||
# 不指定请求方式,返回json数据或字符串
|
||||
@app.route('/page1')
|
||||
def get_cj_name():
|
||||
return json.dumps(data)
|
||||
|
||||
# 指定请求方式,可指定多个以列表字符串形式
|
||||
@app.route('/page2',methods=['POST','OPTIONS'])
|
||||
def registrationInfo():
|
||||
# 返回模板html和数据
|
||||
return render_template('page2.html',data=data)
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 使用https协议
|
||||
#app.run(host='127.0.0.1',port=443,ssl_context=(example.pem,example.key),debug=False)
|
||||
# 使用http协议
|
||||
app.run(host='127.0.0.1', port=5000, debug=True)
|
||||
```
|
||||
# 示例template页
|
||||
```html
|
||||
<!--
|
||||
文件名:template.html
|
||||
文件路径:/templates/template.html
|
||||
用途:用于页面使用同样的header或footer
|
||||
-->
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
|
||||
<title>{% block title%}example标题{% endblock %} </title>
|
||||
<!-- 新 Bootstrap5 核心 CSS 文件 -->
|
||||
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/css/bootstrap.min.css">
|
||||
<!-- 页面中引用static文件的方法,使用url_for('static',filename='文件在static的路径')-->
|
||||
<link rel="stylesheet" href="{{ url_for('static',filename='style.css') }}">
|
||||
<!-- popper.min.js 用于弹窗、提示、下拉菜单 -->
|
||||
<script src="https://cdn.staticfile.org/popper.js/2.9.3/umd/popper.min.js"></script>
|
||||
<!-- 最新的 Bootstrap5 核心 JavaScript 文件 -->
|
||||
<script src="https://cdn.staticfile.org/twitter-bootstrap/5.1.1/js/bootstrap.min.js"></script>
|
||||
|
||||
{% block head %}
|
||||
{% endblock %}
|
||||
</head>
|
||||
<body scroll="no">
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
|
||||
{% block footer %}
|
||||
{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
# 示例内容页
|
||||
```html
|
||||
<!-- 导入模板页 -->
|
||||
{% extends 'template.html' %}
|
||||
|
||||
<!-- content和footer等名字可以自定义,需要和模板页的名字对应即可 -->
|
||||
{% block content %}
|
||||
这里的内容会添加到模板页content处
|
||||
{% endblock %}
|
||||
|
||||
{% block footer %}
|
||||
这里的内容会添加到模板页footer处
|
||||
{% endblock %}
|
||||
|
||||
```
|
||||
# 模板数据
|
||||
```html
|
||||
|
||||
```
|
||||
Reference in New Issue
Block a user