I have a variable set in one view in Fastapi and want to pass it to another one :
from fastapi import APIRouter, Request, Response
from fastapi.templating import Jinja2Templates
templates = Jinja2Templates(directory="templates")
router = APIRouter()
@router.get("/my-first-view")
async def function1(request: Request) -> Response:
"""Display the home page."""
my_variable = value
return templates.TemplateResponse(
"home.jinja",
context={
"my_variable": my_variable
},
)
@router.get("/my-second-view")
async def function2(request: Request, my_variable: str) -> Response:
"""Display the variable processing page."""
return templates.TemplateResponse(
"page.jinja"
)
Normally, this would come to send my_variable
from home.jinja
to page.jinja
.
Thus, in home.jinja
I have the following :
...
<a href="{{url_for('function2', my_variable=my_variable)}}" title="connect">Connect</a>
...
But this is throwing me an error : "starlette.routing.NoMatchFound: No route exists for name \"function2\" and params \"my_variable\".\n"
. I did some researches but I haven't found something really helpful
What is the proper way to do it with Fastapi ? What am I missing ?