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

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

Open
greathector7 opened this issue Dec 20, 2022 · 16 comments
Open

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

greathector7 opened this issue Dec 20, 2022 · 16 comments

Comments

@greathector7
Copy link

greathector7 commented Dec 20, 2022

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
Copy link
Owner

vitalik commented Dec 21, 2022

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

@greathector7
Copy link
Author

greathector7 commented Dec 25, 2022

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
Copy link
Owner

vitalik commented Dec 25, 2022

@greathector7

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

@tboulogne
Copy link

tboulogne commented Jan 30, 2023

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

@baseplate-admin
Copy link
Contributor

baseplate-admin commented Jan 30, 2023

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
Copy link

boxydog commented Mar 6, 2023

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
Copy link

boxydog commented Mar 6, 2023

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
Copy link
Contributor

baseplate-admin commented Mar 7, 2023

@boxydog

I think your error lies with _request

Rename the parameter to request

@boxydog
Copy link

boxydog commented Mar 7, 2023

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
Copy link
Author

greathector7 commented Mar 7, 2023 via email

@boxydog
Copy link

boxydog commented Mar 7, 2023

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
Copy link

boxydog commented Mar 9, 2023

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
Copy link
Contributor

baseplate-admin commented Mar 17, 2023

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
Copy link

noxan commented Mar 1, 2024

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

  • project/urls.py
    from django.urls import include, path
    
    urlpatterns = [
        path("nested/", include("project.nested.urls")),
    ]
  • nested/urls.py
    from django.urls import path
    from .api import api
    
    app_name = "nested"
    urlpatterns = [
         path("api/", api.urls),
    ]
  • nested/api.py
    from ninja import NinjaAPI
    
    api = NinjaAPI()
    
    
    @api.get("/add")
    def add(request, a: int, b: int):
        return {"result": a + b}

@arielaco
Copy link

arielaco commented Mar 8, 2024

@noxan

Try adding urls_namespace="nested:api" to api

@tombohub
Copy link

@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?

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

8 participants