Search code examples
djangodjango-ninja

Method not allowed in fresh django-ninja-extra project


I cant make a post request in my project, now i created a fresh project, because i thought it was a configuration problem. But the Problem persists. The problem is that when i try to make a post request it always gives me a 405 status back.

settings.py:

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'backend.apps.department',
    'ninja_extra'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

api.py:

from ninja_extra import NinjaExtraAPI

api = NinjaExtraAPI()

api.auto_discover_controllers()

department/api.py:

from ninja_extra import api_controller, route
from django.shortcuts import get_object_or_404
from ninja import Schema
from datetime import datetime
from .models import department
from typing import List

class DepartmentIn(Schema):
    name: str
    last_modified: datetime
    user_id: int

class DepartmentOut(Schema):
    id: int
    name: str
    last_modified: datetime

@api_controller('department/')
class DepartmentController:
    
    @route.get("all/", response=List[DepartmentOut])
    def get_departments(self):
        department_list = department.objects.all()
        return department_list

    @route.get("{department_id}/", response=DepartmentOut)
    def get_department(self, department_id: int):
        record = get_object_or_404(department, id=department_id)
        return record
    @route.post("create/")
    def create_department(self, payload: DepartmentIn):
        record = department.objects.create(**payload.dict())
        return {"id": record.id}

models.py:

from django.db import models
from django.conf import settings


class department(models.Model):
    name = models.CharField(max_length=50)
    last_modified = models.DateTimeField(auto_now=True, auto_now_add=False)
    user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

The Problem i get:

Response body:

Method not allowed

Response headers:

 allow: GET 

 content-length: 18 

 content-type: text/html; charset=utf-8 

 cross-origin-opener-policy: same-origin 

 date: Wed,11 Dec 2024 11:22:20 GMT 

 referrer-policy: same-origin 

 server: WSGIServer/0.2 CPython/3.12.5 

 x-content-type-options: nosniff 

 x-frame-options: DENY 

With following request:

curl -X 'POST' \
  'http://127.0.0.1:8000/api/department/create/' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "string",
  "last_modified": "2024-12-11T15:35:25.361Z"
}'```

Solution

  • So i found the Mistake in my Code, i still dont understand why it would be that way, or why it does work but here is my solution to my problem:

    Just move the route.post before the route.get