python - 使用模板时 flask 错误

标签 python flask

<分区>

尝试使用 flask 启动服务器并出现此错误。我的模板文件在模板文件夹(flaskr/templates)

import sqlite3
from flask import Flask, request, session, g, redirect, url_for, \
     abort, render_template, flash

app = Flask(__name__)

@app.route('/templates/')
@app.route('/templates/<name>')
def hello(name=None):
    return render_template('index.html', name=name)

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

我得到错误:

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

最佳答案

一定是你的应用结构有问题

应该是这样的(目录结构)

测试/

----你好.py

----模板/

--------index.html

--------hello.html

你好.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html')

@app.route('/templates/')
@app.route('/templates/<name>')
def hello(name=None):
    return render_template('home.html', name=name)

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

index.html

<h1> Index page</h1>

你好.html

<h1> Home </h1>
<h1> Hello {{name}}</h1>

关于python - 使用模板时 flask 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36194235/

相关文章:

Python:二维列表

python - 从指数的笛卡尔积构建稀疏 COO 矩阵结构

python - 在 plotly 中添加一个 slider 来绘制带有子图的图形

python - 计算 Python 列表中的类实例

python - 设置新的 root_path 后,Flask 找不到静态文件夹

python - Flask-Security 中基于 token 的密码重置功能的说明

apache - 使用在单独的域上运行Flask api和Angularjs前端的Apache正确配置Angularjs和CORS

python - SQLite 插入语句使用 Flask 静默失败

python - 在 Flask 应用 Nose 测试中检查 Flash 消息

python - Flask migrate 未检测到列自动增量更改?