Alex Rivera | Logout

Build error with variables and url_for in Flask

Asked 2012-01-26T18:41:11.080
18

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:
Edit
Report

2 Answers

0

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=...

answered 2012-06-01T17:15:26.883
-3

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')
answered 2012-01-27T07:59:12.277

Your Answer