是否url_for
可以跨蓝图工作?
/flaskapp
/runserver.py (from server import app; app.run(debug=True))
/server
/__init__.py (app = Flask(__name__))
/pages
/__init__.py ('pages' blueprint)
/users
/__init__.py ('users' blueprint)
在server/__init__.py
:
from server.pages import pages
from server.users import users
app = Flask(__name__)
app.register_blueprint(pages)
app.register_blueprint(users)
在server/pages/__init__.py
:
pages = Blueprint('pages', __name__)
@pages.route('/')
def index(): return '<h1>Index</h1>'
在server/users/__init__.py
:
users = Blueprint('users', __name__)
@users.route('/login')
def login():
...
return redirect(url_for('pages.index'))
^^^^^^^^^^^^^^^^^^^^^^
url_for
呼叫引发What BuildError: ('pages.index', {}, None)
would be a way to get to ?'pages.index'
(我尝试导入模块,但没有奏效)