Anyone know how to contruct menu items from variables in flask-gopher?

i.e. I want to link to users by ID, but I get errors like:

Could not build url for endpoint 'user/1'. Did you mean 'user' instead?

I'm going to look up the info on demand, so don't want to have a function hard coded for every possible user...

#gopher #python

it looks like I should be able to use:

@app.route('/user/<userid>')
def get_show_user(userid):

based on:
github.com/michael-lazar/flask

but I'm still getting the same errors 😕

Show thread
Follow

@M0YNG Try this:

@app.route('/user', defaults={userid: None})
@app.route('/user/<userid>')
def get_show_user(userid):
...

and be sure to manage default value for var userid (None or whatever).

Does this solve?

@fabiux hmm, no.

I also had to change 'get_show_user' to 'user' or none of it works (seems to need the function name matching the url?)

This lets me access /user but not /user/1 (same error as before, "Could not build url for endpoint '/user/1'. Did you mean 'user' instead?")

@app.route('/user', defaults={'userid': None})
@app.route('/user/<userid>')
def user(userid):
if userid is None:
<code here>
else:
<more code>

@fabiux yes! I was calling the URL wrong.

I was trying to make '/user/1', but should have been doing this:
url_for('user', userid=instanceInfo.contact_account.id))

Sign in to participate in the conversation
A tech savvy Mastodon

The social network of the future: No ads, no corporate surveillance, ethical design, and decentralization! Own your data with Mastodon!