일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- centos 7.5
- bash
- K8S
- PostgreSQL
- audit
- Audit Log
- zabbix
- RHEL
- docker
- Linux
- CentOS
- Shell
- yum
- Elk
- Elasticsearch
- Kubernetes
- log
- Chrome
- rsyslog
- 빅데이터
- C
- 크롬
- Python
- 서울시민카드
- syslog
- 파이썬
- GPU
- 디렉토리
- GNOME
- JSON
- Today
- Total
Sysops Notepad
django를 이용한 REST API 서버 구축 하기 본문
작업환경 Centos 7.5
REST API 서버 구축하기
https://junebuug.github.io/django-rest-framework01/
https://junebuug.github.io/django-rest-framework02/
python & Django 참고 자료
https://tutorial.djangogirls.org/ko/django_start_project/
https://docs.djangoproject.com/en/1.11/ref/models/fields/#field-types
https://docs.djangoproject.com/ko/2.0/intro/tutorial01/
http://www.django-rest-framework.org/tutorial/3-class-based-views/
앱과 모델 만들기
python manage.py startapp blog
mysite/settings.py 의 INSTALLED_APPS에 blog 추가
python manage.py makemigrations blog
python manage.py migrate blog
python manage.py runserver 0.0.0.0:80
python manage.py createsuperuser
# vi mysite/urls.py
from django.contrib import admin
from django.urls import include, path
from blog.views import post_api
from rest_framework_swagger.views import get_swagger_view
urlpatterns = [
path('',include('blog.urls')),
path('admin/', admin.site.urls),
path('api/doc', get_swagger_view(title='Rest API Document')),
path('api/blog/',post_api.as_view()),
]
# vi blog/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
# vi blog/views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Post
from rest_framework import serializers,mixins
from rest_framework.generics import GenericAPIView
from django.http import HttpResponse
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ('author','title','text','created_date','published_date')
class post_api(GenericAPIView, mixins.ListModelMixin):
queryset = Post.objects.all()
serializer_class = ProductSerializer
def get(self,request,*args,**kwargs):
return self.list(request,*args,**kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
# vi blog/admin.py
from django.contrib import admin
from .models import Post
admin.site.register(Post)
'업무 > dev' 카테고리의 다른 글
[bash] 쉘 스크립트 디렉토리 or 파일 존재 여부 확인하는 법 (0) | 2018.10.31 |
---|---|
Elasticsearch & Slack 연동을 통한 로그 모니터링 하기 (0) | 2018.10.24 |
[bash] 쉘 디렉토리 유무 판단하기 (0) | 2018.09.29 |
[bash] 쉘 반복문 사용하기 (0) | 2018.09.29 |
centos 7 python 3.x 설치 및 virtualenv 설정 (0) | 2018.07.08 |