在 Flask 中使用变量和 url_for 构建错误
在互联网上发现一两个人有类似的问题,但还没有在任何地方看到解决方案。我从下面的代码/模板中收到构建错误,但无法弄清楚问题出在哪里或为什么会发生。模板似乎无法识别该功能,但不知道为什么会发生这种情况。任何帮助将不胜感激 - 我已经敲击键盘两个晚上了。
功能:
@app.route('/viewproj/<proj>', methods=['GET','POST'])
def viewproj(proj):
...
模板摘录:
{% for project in projects %}
<li>
<a href="{{ url_for('viewproj', proj=project.project_name) }}">
{{project.project_name}}</a></li>
{% else %}
No projects
{% endfor %}
错误日志: https://gist.github.com/1684250
编辑:还想包括它无法识别变量“proj”在构建 URL 时,因此它只是附加该值作为参数。这是一个例子: //myproject/viewproj?projname=what+up
最后几行:
[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/
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/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
url_for 查找一个函数,您将要调用的函数的名称传递给它。
所以你应该使用:
我遇到了同样的问题。我按照以下方法解决了它:Flask错误:werkzeug.routing.BuildError
url_for looks for a function, you pass it the name of the function you are wanting to call.
So you should use :
I got the same problem. And I solved it accoring:Flask error: werkzeug.routing.BuildError
刚刚解决了同样的问题,解决方案真的很有趣。
只需添加一个“.”在 url_for 中的方法名称前面。
像这样:
现在应该可以工作了。
Flask 提供的此解决方案的文档为 http://flask.pocoo.org/docs/0.10/api/,我引用:
flask.url_for(endpoint, **values)
使用提供的方法生成给定端点的 URL。
目标端点未知的变量参数将作为查询参数附加到生成的 URL。如果查询参数的值为 None,则跳过整个对。如果蓝图处于活动状态,您可以通过在本地端点前添加点 (.) 来快捷引用同一蓝图。
这将引用当前蓝图本地的索引函数:
Just solved the same problem, the solution is really funny.
Just add a '.' in front of your method name in url_for.
Like this:
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() 的源文件,我也无法理解发生了什么。
一个干净的例子:
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:
查看模板中的“project.project_name”是否正确解析。您是否将“项目”正确传递给模板?相反,对“proj”的一些值进行硬编码,然后查看 url 是否已生成。
像这样的东西:-
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:-
您很可能有多个名为
viewproj
的路由函数。除此之外,您发布的输出与您发布的模板代码不对应。
myproject/viewproj?projname=what+up
表示projname=...
已传递给url_for()
,但您的视图函数需要 <代码>项目=...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 thatprojname=...
was passed tourl_for()
, but your view function expectsproj=...
当您指定该端点上可用的方法时,我认为您必须将所需的方法传递到 url_for 中。
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
但当我想得更多时,我在质疑 url_for 是否在模板中添加了任何值?以下行将为您的锚标记完成相同的操作:
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:
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: