2

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

它的应用层次结构就像

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

两个文件init .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/来自。任何提示表示赞赏。

4

1 回答 1

2

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

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

app.register_blueprint(hello)
于 2012-08-03T02:38:30.133 回答