In my old flask app i have the following:
{% extends "base.html" %} {% block title %}Home{% endblock %} Im converting my app to FastAPI.
The folders 'static' and 'templates' are subfolders from where main.py resides.
main.py:
app = FastAPI()app.add_middleware(GZipMiddleware, minimum_size=500)app.include_router(default.router)#app.include_router(data_mgr_view.router)app.mount("/static", StaticFiles(directory="static"), name="static")app.mount("/templates", Jinja2Templates(directory="templates"), name="templates")default.py:
from alpha_insite.config import tmpltsrouter = APIRouter( prefix="", #dependencies=[Depends(get_token_header)], responses={404: {"description": "resource not found"}},)@router.route('/')async def root(request: Request): context = {"title":"test", "content":f"Place for my charts, studies, etc..."} return tmplts.TemplateResponse(request=request, name="index.html", context=context)index.html:
//{% extends "base.html" %} {% block title %}Home{% endblock %} {% extends "{{ url_for('templates', path='/base.html')}}" %} {% block title %}Home{% endblock %} WHen hitting the root of my url however, i see this:
File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception raise rewrite_traceback_stack(source=source) File "templates/index.html", line 1, in top-level template code {% extends "{{ url_for('templates', path='base.html')}}" %} {% block title %}Home{% endblock %} ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/jinja2/loaders.py", line 204, in get_source raise TemplateNotFound(template)jinja2.exceptions.TemplateNotFound: {{ url_for('templates', path='base.html')}}I've tried both /templates and /base.html as well.So im doing something wrong here. what is the proper way of doing this?