4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Django Ninjaを使ってみた Vol.1(初期設定編)

Last updated at Posted at 2023-01-19

ninja_syuriken_man.png

Django-ninjaがいいという話を聞いたので、ちょっと使ってみた際の備忘録です。
https://zenn.dev/chanmoro/articles/b980d8fa681362

環境
MacOS Ventura
Python 3.10.7
Poetry 1.3.2

グローバルのPythonにインストール

terminal
# pipの場合
pip install django-ninja

# poetryの場合
poetry add django-ninja

プロジェクトを作成したいディレクトリで実行

terminal
django-admin startproject {アプリ名}  # 今回はDjangoNinjaTempleteとする

認証機能を作るためdjango-ninja-jwtをインストール

terminal
# pipの場合
pip install django-ninja-jwt django-ninja-extra six

# poetryの場合
poetry add django-ninja-jwt django-ninja-extra six

PoetryをPython3.10で使う場合はこちらを参考に
https://zenn.dev/hiroga/articles/poetry-env-cannot-use-python3_10

DJANGO_NINJA_TEMPLETEフォルダ(settings.pyがあるディレクトリ)でapi.pyを作成し、以下を貼り付け

DJANGO_NINJA_TEMPLETE/api.py
from ninja_jwt.controller import NinjaJWTDefaultController
from ninja_extra import NinjaExtraAPI

api = NinjaExtraAPI()
api.register_controllers(NinjaJWTDefaultController)
DJANGO_NINJA_TEMPLETE/urls.py
from django.contrib import admin
from django.urls import path, include

from .api import api

urlpatterns = [
    path('api/admin/', admin.site.urls),
    path("api/", api.urls),
]
DJANGO_NINJA_TEMPLETE/settings.py
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ninja_extra', # 追加
]

サーバーを立ち上げて、http://127.0.0.1:8000/api/docs にアクセス

terminal
python manage.py runserver

Fast APIでよく出てくるdocsにアクセスできたら成功
スクリーンショット 2023-01-19 9.03.06.png

こういったサービス運営やってるので、気になる方はぜひ
https://data-lab.project-g.co.jp?utm_source=Qiita&utm_medium=qiita&utm_campaign=qiita_20230119

続きはこちら (2023/03/13追記)
https://qiita.com/ps0317ix/items/ff10e0eddfc24e5db96c

参考文献:
https://django-ninja.rest-framework.com/
https://eadwincode.github.io/django-ninja-jwt/

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?