Django-REST-Basic
1 Setup Batch File
:: # Make project name
set envName=REST_Env
set projectName=REST_Project
set appName=REST_App
:: echo %projectName%
:: # Create the project directory
mkdir %envName%
cd %envName%
:: # Create a virtual environment to isolate our package dependencies locally
python -m venv env
call .\env\Scripts\activate
:: # On Windows use `.\env\Scripts\activate`
:: # On Linux `source .\env/bin/activate`
:: # Install Django and Django REST framework into the virtual environment
pip install django
pip install djangorestframework
:: # Set up a new project with a single application
:: # Note the trailing '.' character
django-admin startproject %projectName% .
cd %projectName%
django-admin startapp %appName%
cd ..
subl %projectName%
:: # Sync DB and Create Admin
python manage.py migrate
python manage.py createsuperuser --email [email protected] --username admin
pause
2 Add REST App and settings to settings.py
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10
}
INSTALLED_APPS = [
...
'rest_framework',
]
3 Add URL and seriazlier.py
urls.py
from django.contrib import admin
from django.urls import include, path
from rest_framework import routers
from REST_Project.REST_App import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
# Wire up our API using automatic URL routing.
# Additionally, we include login URLs for the browsable API.
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
seriazliers.pyfrom django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ['url', 'username', 'email', 'groups']
class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ['url', 'name']
4 Test with CURL
curl -H "Accept: application/json; indent=4" -u admin:admin http://127.0.0.1:8000/users/
5 RESULT
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"url": "http://127.0.0.1:8000/users/1/",
"username": "admin",
"email": "[email protected]",
"groups": []
}
]
}
Reference
この問題について(Django-REST-Basic), 我々は、より多くの情報をここで見つけました https://velog.io/@csk917work/Django-REST-Basicテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol