I have tried numerous combinations to avoid getting a FastAPIError: Prefix and path cannot be both empty (path operation: root)
error when trying to route my website to a separate .py file to separate the app a little. It also does not like having a Prefix of ” or ‘/’.
My goal is to get app.include_router(website.router)
to route to website.py like it does for all the other routes.
main.py
from datetime import datetime, timedelta, timezone
from fastapi import FastAPI, Request, Depends, Form
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from asgi_csrf import asgi_csrf
from routers import, signup, users# ,website
templates = Jinja2Templates(directory="templates")
app = FastAPI(
root_path="/dev"
)
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get('', include_in_schema=False)
@app.get("/")
async def root(request: Request, id: str = ""):
response = templates.TemplateResponse(
request=request, name="index.html", context={"id": id}
)
return response
# app.include_router(website.router)
app.include_router(signup.router, prefix="/signup")
app.include_router(users.router, prefix="/users")
website.py
from datetime import datetime, timedelta, timezone
import uuid
from typing import Annotated
from fastapi import APIRouter, Request, Cookie, Header, Depends, Form
from fastapi.templating import Jinja2Templates
from pydantic import BaseModel
router = APIRouter()
templates = Jinja2Templates(directory="templates")
@router.get('', include_in_schema=False)
@router.get("/")
async def root(request: Request, id: str = ""):
response = templates.TemplateResponse(
request=request, name="index.html", context={"id": id}
)
return response