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] 404 handler override not working as expected #1390

Open
ErikPohl444 opened this issue Jan 12, 2025 · 0 comments
Open

[BUG] 404 handler override not working as expected #1390

ErikPohl444 opened this issue Jan 12, 2025 · 0 comments

Comments

@ErikPohl444
Copy link

Describe the bug

I have a simple demo Django-Ninja app working thanks to a helpful onboarding documentation on the related docs.

All of the endpoints in my simple demo work as expected.

However, the following code in my urls.py file does not produce the expected result based on the docs:

from django.http import Http404
@api.exception_handler(Http404)
def page_not_found(request, exc):
    return api.create_response(
        request,
        {"message": "Please retry later"},
        status=404,
    ) 

I expect my handler to pick up 404s when the server is running, but I get the default

Not Found
The requested resource was not found on this server.

Did a lot of research beforehand and tried a variety of overrides, but none worked-- so am documenting the method from the dn docs:
https://django-ninja.dev/guides/errors/

My code is here:
https://github.com/ErikPohl444/django_ninja_playground

Working control case:
http://127.0.0.1:8000/api/hello_html?name=you

Not working experimental case:
http://127.0.0.1:8000/api/this_endpoint_is_undefined

I've done similar captures of the 404 in Flask and in Django, but I'm hitting a wall here.

It is either:
a) something is buggy with d-n [unlikely]
b) something can be added to for newbies like me to the documentation [potentially]
c) my code is buggy [in which case I might want to make a PR with doc changes --see b -- for folks like me :) ]

Versions (please complete the following information):

  • Python version: [e.g. 3.6]
    Python 3.12.0

  • Django version: [e.g. 4.0]
    Django==5.1.4

  • Django-Ninja version: [e.g. 0.16.2]
    django-ninja==1.3.0

  • Pydantic version: [e.g. 1.9.0]
    pydantic==2.10.4

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