Class Based View
Class Based View
View ํจ์๋ฅผ ๋ง๋ค์ด์ฃผ๋ class
function-based-view
์ ๋น๊ตํ์ ๋ ์ฐจ์ด์ & ์ฅ์ ์ด ์๋ค!GET, POST ์์ฒญ๊ณผ ๊ฐ์ HTTP method๋ฅผ ์กฐ๊ฑด ๋ถ๊ธฐ๊ฐ ์๋ ๊ฐ๊ฐ์ method๋ก ํํ ํ ์ ์๋ค
HTTP method ๊ฐ class method ์ 1:1 ๋์์ด๋ผ ๊ฐ๋ ์ฑ์ด ์ข๋ค
ํ์ฅ์ฑ์ด ๋ฐ์ด๋จ
Mixin
๋ชจ๋์ ํ์ฉํ์ฌ ํ์ฅ์ฑ์ ๊ทน๋ํ ํ ์ ์๋คMixin: class์ ๋ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ด๋ ์ ๋ณด๋ฅผ ์ถ๊ฐํด์ฃผ๊ธฐ ์ํ ๋ชจ๋
๋ก์ง์ด ๋ณต์กํด๋ ์ฝ๋์ ์ง๊ด์ฑ์ ์ ์ง ํ ์ ์์
Function generic view
vs class based generic view
Function generic view
vs class based generic view
Function generic view
ex)
def index(request):
articles = Article.objects.all()
return render(request, 'articles/index.html', {'articles': articles})
class based generic view
ex)
views.py
from django.views.generic import ListView, DetailView
class ArticleListView(ListView):
model = Article
# template_name = 'articles/๋ชจ๋ธ๋ช
_list.html' -> ์ด๋ ๊ฒ ํ๋ฉด ์๋์ผ๋ก ์ฐพ์
# context_object_name = 'object_list'
class ArticleDetailView(DetailView):
model = Article
urls.py
from django.contrib import admin
from django.urls import path
from articles import views
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', views.ArticleListView.as_view()),
path('articles/<int:pk>',views.ArticleDetailView.as_view()),
]
+
Django REST API Auth
https://django-rest-auth.readthedocs.io/en/latest/installation.html
Last updated
Was this helpful?