Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug #3

Open
lewisnjue opened this issue Nov 10, 2024 · 0 comments
Open

Bug #3

lewisnjue opened this issue Nov 10, 2024 · 0 comments

Comments

@lewisnjue
Copy link

lewisnjue commented Nov 10, 2024

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant