Build error with variables and url_for in Flask

怎甘沉沦 提交于 2019-11-29 05:36:35
atupal

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> 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!