Skip to content

[BUG] Router api can not visited #1546

@guozhenyi

Description

@guozhenyi

Describe the bug
When I'm using the interface written by Ninja Router, I can't access it properly.

Demo is here: https://github.com/guozhenyi/django-ninja-bug-20250919

Versions (please complete the following information):

  • Python version: [e.g. 3.8]
  • Django version: [e.g. 4.2]
  • Django-Ninja version: [e.g. 1.4.3]

Note you can quickly get this by runninng this command:

git clone https://github.com/guozhenyi/django-ninja-bug-20250919.git
cd django-ninja-bug-20250919
python manage.py runserver

Use Postman or Bruno, and visit POST /api/users and PUT /api/users.

Create a Ninja router:

apps/core/api.py:

from typing import Optional
from django.http import HttpRequest
from ninja import Router, Schema

router = Router()


class UserPutSchema(Schema):
    id: int
    username: str
    email: Optional[str] = None


@router.put("/users")
def user_update(request: HttpRequest, rdata: UserPutSchema):
    data = {"code": 200}

    return data

Create mysite/api.py:

from typing import Optional
from django.http import HttpRequest
from ninja import NinjaAPI, Schema
from apps.core.api import router as core_router

api = NinjaAPI()


class UserPostSchema(Schema):
    username: str
    password: str
    email: Optional[str] = None


@api.post("/users")
def user_create(request: HttpRequest, rdata: UserPostSchema):
    data = {"code": 200}
    return data

api.add_router("", core_router)

Request result:

Image

Image

Normally, these should all OK.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions