vitalik / django-ninja

💨 Fast, Async-ready, Openapi, type hints based framework for building APIs
https://django-ninja.dev
MIT License
7.07k stars 422 forks source link

'api-1.0.0' is not a registered namespace #637

Open greathector7 opened 1 year ago

greathector7 commented 1 year ago

I had a new distribution on my project:

Api-django ----api ----apps --------persons <-- here is my module ----static

the api function fine but the docs had this message (''api-1.0.0' is not a registered namespace''), and not work

vitalik commented 1 year ago

@greathector7 hard to tell - you might need to give more details

greathector7 commented 1 year ago

i wrote above my distribution on my proyect.

i cant see the docs.

here the error:

Internal Server Error: /ciudadanos/v1/api/docs
Traceback (most recent call last):
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/django/urls/base.py", line 71, in reverse
    extra, resolver = resolver.namespace_dict[ns]
KeyError: 'api-1.0.0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/ninja/openapi/views.py", line 47, in openapi_view
    "openapi_json_url": reverse(f"{api.urls_namespace}:openapi-json"),
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/django/urls/base.py", line 82, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'api-1.0.0' is not a registered namespace
2022-12-25 10:49:57,048 - ERROR -log.py:241 - Internal Server Error: /ciudadanos/v1/api/docs
Traceback (most recent call last):
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/django/urls/base.py", line 71, in reverse
    extra, resolver = resolver.namespace_dict[ns]
KeyError: 'api-1.0.0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/ninja/openapi/views.py", line 47, in openapi_view
    "openapi_json_url": reverse(f"{api.urls_namespace}:openapi-json"),
  File "/home/hrga/.pyenv/versions/venvAPI3913D41/lib/python3.9/site-packages/django/urls/base.py", line 82, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'api-1.0.0' is not a registered namespace
[25/Dec/2022 10:49:57] "GET /ciudadanos/v1/api/docs HTTP/1.1" 500 82166
vitalik commented 1 year ago

@greathector7

could you show some code ? how you initialized NinjaAPI, how you included it to urls ?

tboulogne commented 1 year ago

@vitalik you could have example on #670 . Got the same error when i import from module...

baseplate-admin commented 1 year ago

To be honest, the error shows up whenever we have any form of errors in the arguments that are passed to a decorated function.. This is specially evident if i pass errors in ninja.forms

For example


@api.get('/hello')
def hello(request, name = Form(parameter_that_doesnt_exist=1)):
    return {'hello':'world'}

Raised namespace error for me.

I think this can have a bit more rework instead of throwing a generic error.

boxydog commented 1 year ago

I'm having the same problem ('api-1.0.0' is not a registered namespace when I visit the docs URL, API works fine), and it's not obvious to me how to tell what is making the namespace not register.

Is it possible to turn on warnings, or errors, or check the API code more strictly in some mode?

boxydog commented 1 year ago

My setup is all close to out-of-the-box:

urls.py:

    ...
    path("api/", api.urls),
    ...

api.py:

class MyObjSchema(ModelSchema):
    class Config:
        model = MyObj
        model_fields = ["id", "name"]

@api.get("/myobj", response=list[MyObjSchema])
@paginate
def myobjs(_request):
    return MyObj.objects.all().order_by("id")

It's list instead of List because I have the pyupgrade pre-commit filter rewriting my code. I tried List as well, and that has the same issue.

django-ninja 0.21.0

baseplate-admin commented 1 year ago

@boxydog

I think your error lies with _request

Rename the parameter to request

boxydog commented 1 year ago

Thanks for the guess. Sadly, that didn't help.

FYI, I named it _request so other tools (e.g., PyCharm, flake8, ..) don't complain about an unused variable.

greathector7 commented 1 year ago

Ok thanks for they answer. I Will infórm any detail

El mar., 7 de marzo de 2023 10:57 a. m., baseplate-admin < @.***> escribió:

@boxydog https://github.com/boxydog

I think your error lies with _request

Rename the parameter as request

— Reply to this email directly, view it on GitHub https://github.com/vitalik/django-ninja/issues/637#issuecomment-1458317973, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABK7CAQMBSMVXFK4QIJYKZTW25EGXANCNFSM6AAAAAATE4AWRM . You are receiving this because you were mentioned.Message ID: @.***>

boxydog commented 1 year ago

Again, any sort of automated audit or error message would be really useful. There's something that should be registering that namespace. Can I turn up the logging on that part?

boxydog commented 1 year ago

I found my issue. I put

    path("api/", api.urls),

in my django app urls.py instead of in the django project urls.py.

The API functionality works, because the app urls.py is included in the project urls.py, but the registration for the docs must only be searching the project urls.py.

baseplate-admin commented 1 year ago

FYI, I named it _request so other tools (e.g., PyCharm, flake8, ..) don't complain about an unused variable.

django contribution guide has some information on this

Again, any sort of automated audit or error message would be really useful. There's something that should be registering that namespace. Can I turn up the logging on that part?

I think this is a missing feature that will be highly appreciated. The errors should be more specific

noxan commented 7 months ago

This issue also occurs if you initialize the api in a nested urls.py (which was included in another urls.py). Would be great if that works or at least we should have a hint in the readme that this is not support (if I'm not mistaken?).

Minimal example

arielaco commented 7 months ago

@noxan

Try adding urls_namespace="nested:api" to api

tombohub commented 5 months ago

@noxan How do we use in django apps? Seems like we need to include api.urls in global project path so we can only instatiate NinjaAPI once? and reuse the api object in each app?