from flask import Flask, render_template app = Flask(__name__) @app.route('/news/') #app.route(url) “装饰器” 定义路由 # 一个网址对应一个函数 -> localhost:8005/news ->news() # def news(): # return '这个是新闻' def news(): return render_template('news.html') #函数调用 render_template 渲染templates/news.html到浏览器上 if __name__ == '__main__': app.run(host='0.0.0.0', port = 80, debug = True)
本代码实现的功能是在本地架设一个web后台,当在浏览器中输入localhost:80/news/时显示网页
但初步运行时出现如下错误:
经过百度查询后得知属于端口冲突
将代码改为:
if __name__ == '__main__': app.run(host='0.0.0.0', port = 8005, debug = True)
修改端口成程序成功运行:
在浏览器中输入localhost:8005/news/时显示网页