Flask中url_for()引发werkzeug.routing.BuildError错误的原因咨询
Ah, I know this one—let's break down why you're seeing that BuildError and how to fix it.
The core issue here is Blueprint namespacing in Flask. When you define views inside a Blueprint, Flask automatically adds the Blueprint's name (the first argument you passed to Blueprint(), which is "home_view" in your code) as a prefix to each view's endpoint.
So your index() function in home_view.py doesn't have an endpoint named just index—it's actually registered as home_view.index. The tutorials you referenced probably didn't use Blueprints; they defined views directly in the main Flask app file, where endpoints are just the raw function names. That's why their examples worked without the prefix, but yours doesn't.
You have two simple, clean options:
1. Use the Full Namespaced Endpoint in Your Template
Update your url_for() calls to include the Blueprint prefix explicitly:
<center> <h1>Welcome to my Web Page</h1> </center> <br/><br/> <a href="{{ url_for('home_view.index') }}">Home</a> <a href="{{ url_for('home_view.about') }}">About me</a>
(Note: I added a home link example to match your index view—adjust the about endpoint if that view lives in a different Blueprint.)
2. Use Relative Endpoints (Better for Blueprint-Internal Links)
If this template is only ever rendered by views in the home_view Blueprint, you can use a relative endpoint by prefixing the function name with a dot:
<center> <h1>Welcome to my Web Page</h1> </center> <br/><br/> <a href="{{ url_for('.index') }}">Home</a> <a href="{{ url_for('.about') }}">About me</a>
Flask will automatically resolve this to the current Blueprint's namespace, so .index becomes home_view.index behind the scenes. This keeps your template code cleaner if all links stay within the same Blueprint.
- Without Blueprints: Endpoints = raw function names (e.g.,
index) - With Blueprints: Endpoints =
blueprint_name.function_name(e.g.,home_view.index) - Relative endpoints (
.function_name) work when the template is used exclusively within the same Blueprint.
内容的提问来源于stack exchange,提问作者KZiovas




