python+Flask实现简单的前后端交互

使用Flask需要下载flask插件,直接使用pip install flask即可

 

新建一个文件,内容如下

from flask import Flask

app = Flask(__name__)

@app.route("/show/info")
def index():
    return "test"

if __name__ == '__main__':
    app.run()

运行之后,访问 127.0.0.1:5000/show/info 即可看到 test
如果要返回页面,需要添加 render_template模块,代码下

from flask import Flask,render_template
app = Flask(__name__)
@app.route("/test.html")
def index():
    return render_template("index.html")

@app.route("/new.html")
def newHtml():
    return render_template("wht/new.html")
if __name__ == '__main__':
    app.run()

其中,项目目录如下
所有的html页面都需要在templates文件夹下,启动程序的py文件需要和templates文件夹同级

fristPythonPj/
├── login.py
└── templates
    ├── index.html
    └── wht
        └── new.html

如果想要使用图片,需要创建一个static文件夹,和templates同级,然后再static下创建一个文件夹(名称随意)用于专门存放image

fristPythonPj/
├── login.py
├── static
│   └── img
│       └── 1.jpg
└── templates
    ├── index.html
    └── wht
        └── new.html

html中 img标签内容如下

<img src="/static/img/1.jpeg"/>
posted @ 2022-06-09 17:13  whtjyt  阅读(608)  评论(0编辑  收藏  举报