Flask: Werid behaviour of BuildError

Created on 27 Jul 2017  路  3Comments  路  Source: pallets/flask

Expected Behaviour

I register a route to serve avatar image directly on app:

@app.route('/avatar/<filename>/<size>')
def avatar(filename, size='m'):
    path = current_app.config['AVATARS_UPLOAD_PATH']
    filename = '%s_%s.png' % (filename, size)
    return send_from_directory(path, filename)

Then I use this in template to get avatar:

{{ url_for('avatar', filename=user.avatar, size='l') }}

Actual Behaviour

I have four templates use this code, but only one template got the image, others got the error below:

BuildError: Could not build url for endpoint 'avatar' with values ['size']. Did you forget to specify values ['filename']?
...
File "C:\Users\Administrator\projects\moments-dev\moments\templates\home\index.html", line 16, in block "page_content"
src="{{ url_for('avatar', filename=photo.author.avatar, size='s') }}">
File "c:\users\administrator\projects\moments-dev\venv\lib\site-packages\flask\helpers.py", line 333, in url_for
return appctx.app.handle_url_build_error(error, endpoint, values)
File "c:\users\administrator\projects\moments-dev\venv\lib\site-packages\flask\app.py", line 1805, in handle_url_build_error
reraise(exc_type, exc_value, tb)
File "c:\users\administrator\projects\moments-dev\venv\lib\site-packages\flask\helpers.py", line 323, in url_for
force_external=external)
File "c:\users\administrator\projects\moments-dev\venv\lib\site-packages\werkzeug\routing.py", line 1768, in build
raise BuildError(endpoint, values, method, self)

However, I actually did specify the filename, you can see it in error output.

Here is the templates structure:

templates/
    home/
        - photo.html ---- error
        - index.html ---- error
    user/
        - index.html ---- ok
    macros.html ---- error

I tried to request the URL, such as http://127.0.0.1:5000/avatar/<filename>/<size>, it works fine on every size.

Environment

  • Python version: 2.7
  • Flask version: 0.12.2
  • Werkzeug version: 0.12.2
  • OS: Windows 7

Most helpful comment

is user.avatar None?

All 3 comments

is user.avatar None?

Oh, there are some user.avatar on these three templates was None! Thanks!

I have encountered the same problem:
The endpoint value has one is None, so I got this problem.
Thank you.

Was this page helpful?
0 / 5 - 0 ratings