the code for me did not work for the handlers.py i hade to do this
this is because of the circular import of app
in handlers.py :
def get_handlers():
from starlette.exceptions import HTTPException as StarletteHTTPException
from app.main import app
from app.shortcuts import render, redirect, is_htmx
from app.users.exceptions import LoginRequiredException
# this is how am handling the http exception by changing it please dont forget how we are doing this since it is very very important
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request, exc):
status_code = exc.status_code
template_name = 'errors/main.html'
if status_code == 404:
template_name = 'errors/404.html'
context = {"status_code": status_code}
return render(request, template_name, context, status_code=status_code)
@app.exception_handler(LoginRequiredException)
async def login_required_exception_handler(request, exc):
response = redirect(f"/login?next={request.url}", remove_session=True)
if is_htmx(request):
response.status_code = 200
response.headers['HX-Redirect'] = f"/login"
return response
and then inside main.py i changed the import to
from .handlers import get_handlers
get_handlers()
the code for me did not work for the handlers.py i hade to do this
this is because of the circular import of app
in handlers.py :
and then inside main.py i changed the import to