0

I want to use path parameters along with format strings.

prefix = 'api/v1'

@router.get(f'{prefix}/{item}')
def get_item(item: str):
    return ...

Here, python complains about there not being an item variable. Replacing f'{prefix}/{item} with '%s/{item}' % (prefix) probably fixes it, but it looks quite ugly, is there a better way to do it?

sharpnife
  • 198
  • 12
  • 1
    If you don't want the format string to expand the `{`, use `{{`: `@router.get(f'{prefix}/{{item}}')`. This will give the literal string `{item}` to the decorator function. – MatsLindh Feb 23 '23 at 09:01
  • add prefix in your app `app.include_router(router, prefix=prefix)` and you can just write `@router.get("/{item}")` – Artem Strogin Feb 23 '23 at 20:42

0 Answers0