用Flask中的变量和url_for构建错误 [英] Build error with variables and url_for in Flask
问题描述
函数:
@ app.route('/ viewproj / < proj>,methods = ['GET','POST'])
def viewproj(proj):
模板摘录:
{%项目中的项目%}
< li>
< a href ={{url_for('viewproj',proj = project.project_name)}}>
{{project.project_name}}< / a>< / li>
{%else%}
没有项目
{%endfor%}
错误日志:
https://gist.github.com/1684250 p>
编辑:也想包含在构建URL时不会识别变量proj,所以它只是作为参数追加值。下面是一个例子:
// 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 ,第103行,在顶级模板代码中,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,第34行,在blockbody中,referer:xx://myproject.com/
[Wed Jan 25 09:47:34 2012] [error] [客户端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]文件/usr/lib/python2.7/dist-packages/flask/helpers.py,第195行,在url_for,referer:xx:// myproject .com /
[Wed Jan 25 09:47:34 2012] [错误] [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]文件/usr/lib/pymodules/python2.7/werkzeug/routing.py,行1409,在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 :'viewproj',{'proj':'12th'},None),referer:xx://myproject.com/
url_for寻找一个函数,把它传递给函数的名字想打电话。
所以你应该使用:
$ p $ {{url_for('viewproj',proj = xxx)}}
我遇到同样的问题。我解决了这个问题:烧瓶错误:werkzeug.routing.BuildError
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
这篇关于用Flask中的变量和url_for构建错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!