Chapter 2
Library Website and API
Traditional Django
전통적인 Django와 Django REST Framework의 차이
Django는 웹 사이트를 만들고,
Django REST Framework는 web API를 만든다.
$ pipenv install django==2.2.6
$ django-admin startproject library_project .
$ python manage.py migrate
$ python manage.py runserver
First app
$ python manage.py startapp books
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Local
'books.apps.BooksConfig', # new
]
Models
from django.db import models
# Create your models here.
class Book(models.Model):
title = models.CharField(max_length=250)
subtitle = models.CharField(max_length=250)
author = models.CharField(max_length=100)
isbn = models.CharField(max_length=13)
def __str__(self):
return self.title
Admin
$ python manage.py createsuperuser
from django.contrib import admin
from .models import Book
admin.site.register(Book)
Views
from django.shortcuts import render
from django.views.generic import ListView
# Create your views here.
from .models import Book
class BookListView(ListView):
model = Book
template_name = 'book_list.html'
URLs
from django.contrib import admin
from django.urls import path, include # new
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('books.urls')), # new
]
from django.urls import path
from .views import BookListView
urlpatterns = [
path('', BookListView.as_view(), name='home'),
]
<h1>All books</h1>
{%for book in object_list%}
<ul>
<li>Title : {{book.title}}</li>
<li>Subtitle : {{book.subtitle}}</li>
<li>Author : {{book.author}}</li>
<li>ISBN : {{book.isbn}} </li>
</ul>
{%endfor%}
Django REST Framework
$ pip install djangorestframework
INSTALLED_APPS = [
'api.apps.ApiConfig', #new app
'rest_framework', #3rd party, new
'books.apps.BooksConfig', # new, local
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
$ python manage.py startapp api
URLs
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('api/', include('api.urls')),
path('', include('books.urls')),
path('admin/', admin.site.urls),
]
from django.urls import path
from .views import BookAPIView
urlpatterns = [
path('', BookAPIView.as_view()),
]
Views
from rest_framework import generics
from books.models import Book
from .serialziers import BookSerializer
class BookAPIView(generics.ListAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
# Create your views here.
Serializers
from rest_framework import serializers
from books.models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = ('title', 'subtitle', 'author', 'isbn')
Browsable API
'Computer Science > BackEnd' 카테고리의 다른 글
Django for APIs | #5 blog API (0) | 2022.09.30 |
---|---|
Django for APIs | #4 Todo API (0) | 2022.09.27 |
Django for APIs | #2 Web APIs (1) | 2022.09.26 |
Django for APIs | #0 Introduction (0) | 2022.09.18 |
Django API #5 | More views and viewsets (0) | 2022.09.12 |