0
votes

I have this line in my code:

    print "Location: ", url_for('get_user', id=9, _external=True)

The function to that shoudl be this one:

@app.route('/api/users/<int:id>')
def get_user(id):
    print "Hello: ", id
    user = User.query.get(id)
    if not user:
        abort(400)
    return jsonify({'username': user.firstname})

(The Hello was just for me to see if that function gets triggered - which it doesn`t)

Then the error is this:

File "C:\Python27\lib\site-packages\werkzeug\routing.py", line 1616, in build


raise BuildError(endpoint, values, method)

BuildError: ('get_user', {}, None)

Do you have any idea why is that?

Thanks for your help!!!

PS.: If it helps I was initially using the tutorial about building an API with Flask by Miguel Grinberg which worked in the first place but then I played around and that was it...

2
Did you import get_user in module, where you call print? - Jimilian
It is all the same file. - Kev
What version of flask and werkzeug do you use? - Jimilian
Can you give us the full traceback of the exception? The error shows that you are not passing in id=9 (the second argument, values, is empty). - Martijn Pieters
@Jimilian: no, it cannot find a method that doesn't take parameters. That's something different. If the same function was registered with @app.route('/api/users/', defaults={'id': -1}) for example, you can use url_for() without specifying an id value. - Martijn Pieters

2 Answers

0
votes

url_for('get_user', _external=True, id=9) works for me. But get_user must be visible for url_for.

0
votes

When I checked it today it worked and I do not know what was the problem yesterday...sorry - but many thanks for your suggestions.