r/FastAPI • • Oct 18 '20

Jinja2 TemplateNotFound Error

I'm trying to access the "/" route however I keep on getting TemplateNotFound despite it being in the right directory.

main.py:

from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
app = FastAPI()
from app.routers import home, auth
app.include_router(home.router)

home.py:

from fastapi import APIRouter, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates

router = APIRouter()
router.mount("/app/static", StaticFiles(directory="app/static"), name="static")

templates = Jinja2Templates(directory="templates")

@router.get("/")
async def homepage(request: Request):
    return templates.TemplateResponse("home/index.html", {"request": request})

Directory Layout: https://i.imgur.com/qJjZTvs.png

0 Upvotes

1 comment sorted by

1

u/HEVEL-H Feb 05 '22

from fastapi import Request
from fastapi.routing import APIRouter
from fastapi.responses import HTMLResponse
from os.path import abspath
from fastapi.templating import Jinja2Templates
router = APIRouter(prefix='/view', tags=['view'])
templates = Jinja2Templates(directory=("./app/templates"))

(a)router.get("/", response_class=HTMLResponse) #
async def index(request: Request):
print('veamos')
return templates.TemplateResponse('index.html', {"request": request, "id": "asd"})

# put the decorator this is an efficient solution you can explain it then ami serves me