ref. 2 를 보고 따라하면 된다.
myall 이라는 이름의 application 을 만드는 경우
d:\mine\programming\python\myprojectroot\myproject>d:\envs\myproject\Scripts\django-admin.py startapp myall
INSTALLED_APPS = ( ... 'myall', )
Url 설정
application 이름을 rapi 라고 가정 하자.<project_name>/project_name/urls.py
urlpatterns = [
...
url(r'^rapi/', include('rapi.urls', namespace='rapi')),
]
Django 3.0
project_name/urls.py
<project_name>/project_name/urls.pyurlpatterns = [
...
path('rapi/', include('rapi.urls', namespace='rapi'))
]
<app>/urls.py
from django.urls import path, include
from django.contrib import admin
from django.views.generic import TemplateView
# from riotapi.views import LStatRuneView, RunePageView
"""
lolstat.urls
- path('rapi/', include('rapi.urls', namespace='rapi')),
"""
app_name = "rapi"
urlpatterns = [
path('', SimpleApiView.as_view()),,
]
django-model-utils 설치
(myproject) d:\mine\programming\python\myprojectroot\myproject>pip install django-model-utils
Model 만들기
makemigrations
먼저 migrate 할 녀석을 만든다.
(myproject) d:\mine\programming\python\myprojectroot\myproject>manage.py makemigrations handsetcare --settings=myproject.settings.local Migrations for 'handsetcare': 0001_initial.py: - Create model RemittanceReceiver
migrate
그 다음 실제로 db 에 적용한다.(myproject) d:\mine\programming\python\myprojectroot\myproject>manage.py migrate --settings=myproject.settings.local Operations to perform: Synchronize unmigrated apps: staticfiles, debug_toolbar, messages Apply all migrations: sessions, admin, sites, auth, handsetcare, contenttypes Synchronizing apps without migrations: Creating tables... Running deferred SQL... Installing custom SQL... Running migrations: Rendering model states... DONE Applying contenttypes.0001_initial... OK Applying auth.0001_initial... OK Applying admin.0001_initial... OK Applying contenttypes.0002_remove_content_type_name... OK Applying auth.0002_alter_permission_name_max_length... OK Applying auth.0003_alter_user_email_max_length... OK Applying auth.0004_alter_user_username_opts... OK Applying auth.0005_alter_user_last_login_null... OK Applying auth.0006_require_contenttypes_0002... OK Applying handsetcare.0001_initial... OK Applying sessions.0001_initial... OK Applying sites.0001_initial... OK
User Model 확장
기본적으로 Django 에 있는 User 계정에 자신이 원하는 정보를 추가로 저장하게 할 수 있다. 관련해서는 아래 글을 참고하자.Django REST framework 설치
이 Django REST framework 는 설치해서 사용하면 많은 것을 알아서 해준다. 그런데 이해하는데에 시간이 좀 필요하다.
참고 :
- tutorial
- https://github.com/i5on9i/django-restframework-init : 간단한 RestFramework 예제
- i5on9i/django-restframework-init2 : django 3.0
(myproject) d:\mine\programming\python\myprojectroot\myproject>pip install djangorestframework
이 api-auth 를 넣어주면, browser 에서 api 가 정상 동작하는지 볼 수 있다.
urlpatterns = [ ... url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]
// settings.py
INSTALLED_APPS = [ ... 'rest_framework', ]
########## REST_FRAMEWORK CONFIGURATION # See: http://www.django-rest-framework.org/#installation REST_FRAMEWORK = { # Use Django's standard `django.contrib.auth` permissions, # or allow read-only access for unauthenticated users. 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' ] } ########## END REST_FRAMEWORK CONFIGURATION
Authentication & Permission
cheet sheet
User Login Form
가장 기본적인 기능인 user login 이나 sign up 에 관해서는 아래 글을 참고하자.
Reference
- django-admin and manage.py | Django documentation | Django
- Writing your first Django app, part 1 | Django documentation | Django
- Django REST Framework 3.2 -- Classy DRF
댓글 없음:
댓글 쓰기