用Flask中的变量和url_for构建错误 [英] Build error with variables and url_for in Flask

查看:239
本文介绍了用Flask中的变量和url_for构建错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发现有一两个人遇到类似的问题,但还没有找到解决办法。我从下面的代码/模板得到一个构建错误,但不能找出问题出在哪里或为什么发生。看起来该模板不能识别该功能,但不知道为什么会发生这种情况。任何帮助将不胜感激 - 已经敲了我的键盘两个晚上现在。

函数:

  @ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆