Have found one or two people on the interwebs with similar problems, but haven't seen a solution posted anywhere. I'm getting a build error from the code/template below, but can't figure out where the issue is or why it's occurring. It appears that the template isn't recognizing the function, but don't know why this would be occurring. Any help would be greatly appreciated - have been pounding my against the keyboard for two nights now.
Function:
@app.route('/viewproj/<proj>', methods=['GET','POST'])
def viewproj(proj):
...
Template Excerpt:
{% for project in projects %}
<li>
<a href="{{ url_for('viewproj', proj=project.project_name) }}">
{{project.project_name}}</a></li>
{% else %}
No projects
{% endfor %}
Error log: https://gist.github.com/1684250
EDIT: Also wanted to include that it's not recognizing the variable "proj" when building the URL, so it's just appending the value as a parameter. Here's an example: //myproject/viewproj?projname=what+up
Last few lines:
[Wed Jan 25 09:47:34 2012] [error] [client 199.58.143.128] File "/srv/www/myproject.com/myproject/templates/layout.html", line 103, in top-level template code, referer: xx://myproject.com/
[Wed Jan 25 09:47:34 2012] [error] [client 199.58.143.128] {% block body %}{% endblock %}, referer: xx://myproject.com/
[Wed Jan 25 09:47:34 2012] [error] [client 199.58.143.128] File "/srv/www/myproject.com/myproject/templates/main.html", line 34, in block "body", referer: xx://myproject.com/
[Wed Jan 25 09:47:34 2012] [error] [client 199.58.143.128] <a href="{{ url_for('viewproj', proj=project.project_name) }}">, referer: xx://myproject.com/
[Wed Jan 25 09:47:34 2012] [error] [client 199.58.143.128] File "/usr/lib/python2.7/dist-packages/flask/helpers.py", line 195, in url_for, referer: xx://myproject.com/
[Wed Jan 25 09:47:34 2012] [error] [client 199.58.143.128] return ctx.url_adapter.build(endpoint, values, force_external=external), referer: xx://myproject.com/
[Wed Jan 25 09:47:34 2012] [error] [client 199.58.143.128] File "/usr/lib/pymodules/python2.7/werkzeug/routing.py", line 1409, in build, referer: xx://myproject.com/
[Wed Jan 25 09:47:34 2012] [error] [client 199.58.143.128] raise BuildError(endpoint, values, method), referer: xx://myproject.com/
[Wed Jan 25 09:47:34 2012] [error] [client 199.58.143.128] BuildError: ('viewproj', {'proj': '12th'}, None), referer: xx://myproject.com/
url_for looks for a function, you pass it the name of the function you are wanting to call. So you should use :
{{ url_for('viewproj', proj=xxx) }}
I got the same problem. And I solved it accoring:Flask error: werkzeug.routing.BuildError
Just solved the same problem, the solution is really funny.
Just add a '.' in front of your method name in url_for.
Like this:
<a href="{{ url_for('.viewproj', proj=project.project_name) }}">
And it should work now.
The document for this solution from Flask is http://flask.pocoo.org/docs/0.10/api/, and I quote:
flask.url_for(endpoint, **values) Generates a URL to the given endpoint with the method provided.
Variable arguments that are unknown to the target endpoint are appended to the generated URL as query arguments. If the value of a query argument is None, the whole pair is skipped. In case blueprints are active you can shortcut references to the same blueprint by prefixing the local endpoint with a dot (.).
This will reference the index function local to the current blueprint:
url_for('.index')
I still reply to this even though it kind of has been answered already. The reason is for clarity. Even after reading them, I couldn't understand what was going on without looking at the source file for url_for().
A clean example:
<a href="{{ url_for('viewproj', proj='<projname>') }}">Project name</a>
@app.route('/viewproj/<projname>', methods=['GET','POST'])
def viewproj(proj):
See if 'project.project_name' is resolving correctly in the template. Are you passing 'projects' correctly to template? Hard code some value for 'proj' instead and see the url is getting generated. Something like:-
<a href="{{ url_for('viewproj', proj='new_project') }}">new project</a>
You most likely have more than one routed function with the name viewproj
.
Besides that, the output you posted does not correspond with the template code you posted. myproject/viewproj?projname=what+up
means that projname=...
was passed to url_for()
, but your view function expects proj=...
Seeing as you specify which methods are available on that endpoint I think you will have to pass which method you want into url_for.
url_for('viewproj', proj=project.project_name, method='GET')
I googled for the same problem and found this, so I thought I would post what worked for me after I banged at it for a bit (In case anyone else landed here). Looks like it may just be a string concatenation issue
I had incorrectly "translated" my working code:
{% for project in projects %}
<li>
<a href="{{ url_for('viewproj', proj='%s') }}"|format(project.project_name)>
{{project.project_name}}</a>
</li>
{% else %}
No projects
{% endfor %}
Interesting effect of the code above is some "padding?" added to the link url
But as I thought more about it, I was questioning whether the url_for adds any value in the template? The following line will accomplish the same thing for your anchor tag:
<a href="viewproj/{{ project.project_name }}">{{ project.project_name }}</a>
来源:https://stackoverflow.com/questions/9023488/build-error-with-variables-and-url-for-in-flask