Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use url_for with routers and shared templates/static files #2698

Closed
8 tasks done
legalla opened this issue Jan 25, 2021 · 4 comments
Closed
8 tasks done

How to use url_for with routers and shared templates/static files #2698

legalla opened this issue Jan 25, 2021 · 4 comments

Comments

@legalla
Copy link

legalla commented Jan 25, 2021

First check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.
  • [ x After submitting this, I commit to one of:
    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
    • Implement a Pull Request for a confirmed bug.

Description

I would like to use the following organisation :

.
| --- __init__.py
| --- main.py
| --- templates/
|          |--- includes/
|          |--- <some_files>.html
|---- routers/
|          | --- api/
|          | --- dhs/
|                 | --- __init__.py   
|                 | --- dhs.py

for API routers, everything seems to work fine.
But I have some issues with the dhs part.
main.py hosted some functions which have to render templates, as well as dhs.py.
But as I want to host my templates/static files in only place, I've let them in the templates and static folders as described above.
Here are the code i'm using within main.py and dhs.py :

main.py

templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")

dhs.py

templates = Jinja2Templates(directory="./templates")
dhs.mount("/static", StaticFiles(directory="./static"), name="static")

But with this, as I'm using some url_for which points to functions in dhs.py, it fails with the following error :

engine | <li><a href="{{ url_for('.dhs_configuration') }}">DHS Configuration</a></li>
engine | File "/usr/local/lib/python3.7/site-packages/starlette/templating.py", line 59, in url_for
engine | return request.url_for(name, **path_params)
engine | File "/usr/local/lib/python3.7/site-packages/starlette/requests.py", line 162, in url_for
engine | url_path = router.url_path_for(name, **path_params)
engine | File "/usr/local/lib/python3.7/site-packages/starlette/routing.py", line 494, in url_path_for
engine | raise NoMatchFound()
engine | starlette.routing.NoMatchFound

for the url_for, I've tried multiple options :

{{ url_for('.dhs_configuration') }}
{{ url_for('./routers/dhs/dhs_configuration') }}
{{ url_for(dhs.dhs_configuration') }}
etc...

But nothing seems to work.

Am I missing something ?
What could be the issue ?
What would the Best Practices in order to use shared templates/static files with multiple routers ?

Thanks a lot for your support.

Environment

  • OS: Tiangolo FastAPI docker.

  • Python version:

# python --version
Python 3.7.7
@legalla legalla added the question Question or problem label Jan 25, 2021
@Dustyposa
Copy link
Contributor

What is your path code?
like this?

@app.get("/items/{id}", response_class=HTMLResponse)
async def read_item(request: Request, id: str):
    return templates.TemplateResponse("item.html", {"request": request, "id": id})

@legalla
Copy link
Author

legalla commented Jan 29, 2021

Hi,

Yes, absolutely.
The dhs-configuration is a function defined like this.
When I don't use the hierarchy, meaning everything is placed in app/, everything works fine.

@tiangolo
Copy link
Owner

Thanks for the help here @Dustyposa ! 👏 🙇

@legalla if you are still having problems, please create a minimal, self-contained example, that shows your error. Probably a couple of files, that I can copy paste to replicate it here. If you solved your problem, you can close the issue. ✔️

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@github-actions
Copy link
Contributor

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

@tiangolo tiangolo reopened this Feb 27, 2023
Repository owner locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6986 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants