gpt4 book ai didi

python - 使用蓝图在 Flask 中复制 url_prefix

转载 作者:行者123 更新时间:2023-12-03 16:46:05 25 4
gpt4 key购买 nike

抱歉,我已经在 stackoverflow 中搜索并用谷歌搜索,但没有找到有用的信息。
我有一个 flask 应用程序,
python 版本2.6
flask 版本 0.9

它的应用层次结构就像

  application/
__init__.py
app.py
hello/
__init__.py
view.py
templates/
hello.html

两个文件 初始化 .py 为空
app.py
-----------------------
from flask import Flask
from flup.server.fcgi import WSGIServer
from hello.view import hello

app = Flask(__name__)
app.debug = True

app.register_blueprint(hello, url_prefix='/hello')

if __name__ == '__main__':
WSGIServer(app, bindAddress='/tmp/app.sock').run()
view.py
-----------------------
import os
from flask import Blueprint, render_template, abort
from jinja2 import TemplateNotFound

hello = Blueprint('hello', __name__, template_folder='templates')

@hello.route('/')
def get_index():
try:
return render_template('hello.html')
except TemplateNotFound:
abort(404)
hello.html
-----------------------
<!DOCTYPE html>
<html>
<head>
{% block head %}
<meta charset="utf-8">
{% endblock %}
</head>
<body>
<div>
{% block body %}
<h1><a href="{{ url_for('hello.get_index') }}">Click Me</a></h1>
{% endblock %}
</div>
</body>
</html>

当我输入 localhost:8080/hello 时它工作正常,但如果我单击 html 中的链接,则会出现错误。我发现它的 url 值为 href="/hello/hello/" (应该是 /hello/ 对吗?)。
我知道 hello.get_index 映射到 /hello/ ,但不知道第一个 hello/来自。任何提示表示赞赏。

最佳答案

您在注册蓝图时是否尝试过删除 url_prefix 参数?例如,如果您将以下内容更改为:

app.register_blueprint(hello, url_prefix='/hello')


app.register_blueprint(hello)

关于python - 使用蓝图在 Flask 中复制 url_prefix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11663244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com