I've faced this problem with all APIs for any application in the project. To be honest, I've no idea what triggers the problem. Probably, wrong import module or mismatch in urls.
I'd like to show you my project structure for better understadning: Project structure
Here's my code from news/views.py:
from asgiref.sync import sync_to_async
# Create your views here.
from django.core.exceptions import ObjectDoesNotExist
from ninja import Router
from ninja.errors import HttpError
from .models import News
from .schemas import NewsIn
router = Router(tags=["News"])
@router.post("/")
async def create_news(request, data: NewsIn):
news = await sync_to_async(News.objects.create)(**data.dict())
return {"id": news.id}
@router.put("/{news_id}/")
async def update_news(request, news_id: int, data: NewsIn):
try:
news = await sync_to_async(News.objects.get(id=news_id))
for key, value in data.dict().items():
setattr(news, key, value)
await sync_to_async(news.save())
return {"success": True}
except ObjectDoesNotExist:
raise HttpError(404, "News not found")
@router.delete("/{news_id}/")
async def delete_news(request, news_id: int):
try:
news = await sync_to_async(News.objects.get(id=news_id))
await sync_to_async(news.delete())
return {"success": True}
except ObjectDoesNotExist:
raise HttpError(404, "News not found")
And then code from core/urls.py:
`from django.contrib import admin
from django.urls import path
from ninja import NinjaAPI
from news.views import router as news_router
from partners.views import router as partners_router
from educational_programs_and_categories.views import program_router, category_router
api = NinjaAPI()
urlpatterns = [
path("admin/", admin.site.urls),
path("api/", api.urls),
]
api.add_router("news", news_router)
api.add_router("partners", partners_router)
api.add_router("educational_programs", program_router)
api.add_router("categories_of_educational_programs", category_router)`
Error from SwaggerUI (OpenAPI):SwaggerUI 404 error
I've tried many things from ChatGPT, Internet and nothing helped. Feel free to advice anything and I'll try it.