목차
0. 시작하며
1. FBV(Function Based View) : 함수 기반 뷰
1.1. View에서 자주 쓰이는 함수
1.1.1. redirect()
1.1.2. render()
1.1.3. reverse()와 reverse_lazy()
2. CBV(Class Based View) : 클래스 기반 뷰
2.1. TemplateView
2.2. FormView
2.3. ListView
2.4. CreateView
View의 가장 중요한 역할 중 하나는 모델을 통해 DB의 데이터를 잘 가져오는 것이다. 그래야 클라이언트가 원하는 데이터를 보내줄 수 있기 때문이다. View는 무조건 HttpResponse 개체를 반환해야한다. 그래야 view를 감싸는 middleware에서 문제를 일으키지 않는다. View에서 기능하는 것은 2가지 방식으로 나타낼 수 있다.
1. FBV : 함수 기반 뷰
2. CBV : 클래스 기반 뷰
바로 예제를 살펴보자.
from django.urls import path
from .views import my_function_based_view, MyClassBasedView
urlpatterns = [
path('fbv/', my_function_based_view, name='fbv_example'),
path('cbv/', MyClassBasedView.as_view(), name='cbv_example'),
]
from django.shortcuts import render
from django.http import HttpResponse
def my_function_based_view(request):
return HttpResponse("함수 기반 뷰 입니다.")
from django.shortcuts import get_object_or_404, render
from .models import Post
def post_detail(request, pk):
post = Post.objects.get(pk=pk)
# post = get_object_or_404(Post, pk=pk) #예외 발생시 페이지 없음(404)을 보여줌으로써 예외처리
return render(request, 'board/post_detail.html', {
'post': post,
})
def my_view(request):
my_list = ['항목1', '항목2', '항목3'] # 리스트로도 context를 정의 가능
return render(request, 'my_template.html', {
'my_list': my_list
})
def my_view(request):
context = {'my_variable': '안녕하세요, Django!'}
return render(request, 'my_template.html', context)
ex) redirect('/')
ex) redirect(instance)
모델 개체(인스턴스)를 인자로 넣었을 때, 그 모델 개체에 get_absolute_url()을 지정되어 있다면, 해당 경로로 이동
def redirect(to, *args, permanent=False, **kwargs):
"""
Return an HttpResponseRedirect to the appropriate URL for the arguments
passed.
The arguments could be:
* A model: the model's `get_absolute_url()` function will be called.
* A view name, possibly with arguments: `urls.reverse()` will be used
to reverse-resolve the name.
* A URL, which will be used as-is for the redirect location.
Issues a temporary redirect by default; pass permanent=True to issue a
permanent redirect.
"""
redirect_class = (
HttpResponsePermanentRedirect if permanent else HttpResponseRedirect
)
return redirect_class(resolve_url(to, *args, **kwargs))
resolve_url로 한 단계 더 들어가보자.
def resolve_url(to, *args, **kwargs):
"""
Return a URL appropriate for the arguments passed.
The arguments could be:
* A model: the model's `get_absolute_url()` function will be called.
* A view name, possibly with arguments: `urls.reverse()` will be used
to reverse-resolve the name.
* A URL, which will be returned as-is.
"""
# If it's a model, use get_absolute_url()
# 모델 개체(인스턴스)에 멤버함수로 get_absolute_url()이 있다면 사용
if hasattr(to, "get_absolute_url"):
return to.get_absolute_url()
if isinstance(to, Promise):
# Expand the lazy instance, as it can cause issues when it is passed
# further to some Python functions like urlparse.
to = str(to)
# Handle relative URLs
if isinstance(to, str) and to.startswith(("./", "../")):
return to
# Next try a reverse URL resolution.
try:
return reverse(to, args=args, kwargs=kwargs)
except NoReverseMatch:
# If this is a callable, re-raise.
if callable(to):
raise
# If this doesn't "feel" like a URL, re-raise.
if "/" not in to and "." not in to:
raise
# Finally, fall back and assume it's a URL
return to
1번째 인자 : request
2번째 인자 : 어느 Template(html 파일)로 보낼 것인지
3번째 인자 : 뭘 보낼 것인지(선물 하나 딕셔너리에 포장해서 보내는 것)
도착한 template에서 딕셔너리의 키로 선물 뜯어서 사용 가능 ex) {{ data }}
render(request, 'app_name/index.html', {
'data': data,
})
def render(
request, template_name, context=None, content_type=None, status=None, using=None
):
"""
Return an HttpResponse whose content is filled with the result of calling
django.template.loader.render_to_string() with the passed arguments.
"""
content = loader.render_to_string(template_name, context, request, using=using)
return HttpResponse(content, content_type, status)
#urls.py
from django.urls import path
from . import views
urlpatterns = [
path('articles/', views.article_list, name='article-list'),
path('articles/<int:article_id>/', views.article_detail, name='article-detail'),
]
urls.py에 정의된 name 매개변수를 사용하여 url을 얻어냄
#views.py
from django.urls import reverse
url1 = reverse('article-list')
url2 = reverse('article-detail', args=[1])
print(url1) # 출력: '/articles/'
print(url2) # 출력: '/articles/1/'
CBV에서 사용되는 reverse()
#views.py
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
from .models import MyModel
class MyModelCreateView(CreateView):
model = MyModel
fields = ['name']
def get_success_url(self):
return reverse_lazy('blog:article-list')
공통 기능 : 상속 문법 사용
from django.views import View
from django.http import HttpResponse
class MyClassBasedView(View):
def get(self, request):
return HttpResponse("클래스 기반 뷰 입니다.")
2.1.1.1. 멤버변수
template_name
2.1.1.2. 멤버함수
def get(self, request, *args, **kwargs):
def get_context_data(self, **kwargs):
#django\views\generic\base.py
def get_context_data(self, **kwargs):
kwargs.setdefault("view", self)
if self.extra_context is not None:
kwargs.update(self.extra_context)
return kwargs
커스텀하기
: kwargs를 return 한다는 것은 딕셔너리를 반환한다는 것(bcz. 매개변수에 ** 붙어있음)
def get_context_data(self, **kwargs):
financial_products = FinancialProduct.objects.all()
kwargs['financial_products'] = financial_products # return할 딕셔너리에 데이터 추가
return super().get_context_data(**kwargs)
https://ccbv.co.uk/projects/Django/5.0/django.views.generic.base/TemplateView/
https://github.com/django/django/blob/5.0/django/views/generic/base.py#L220
2.2.1.1. 멤버변수
form_class
success_url
template_name
2.2.1.2. 멤버함수
def form_valid(self, form):
class YourFormView(FormView):
template_name = 'your_template.html' # 사용할 템플릿 파일의 경로
form_class = YourForm # 사용할 폼 클래스
success_url = '/success/' # 폼 제출 후 이동할 URL
def form_valid(self, form):
# 폼이 유효할 때 실행되는 로직을 여기에 추가
# form.cleaned_data를 통해 폼의 정제된 데이터에 접근 가능
return super().form_valid(form)
def form_invalid(self, form):
# 폼이 유효하지 않을 때 실행되는 로직을 여기에 추가
return super().form_invalid(form)
https://ccbv.co.uk/projects/Django/5.0/django.views.generic.edit/FormView/
https://github.com/django/django/blob/5.0/django/views/generic/edit.py#L165
2.3.1.1. 멤버변수
model
template_name
context_object_name
: 지정하지 않으면 기본값은 'object_list' -> 이것을 템플릿에서 사용가능 {{ object_list }}
paginate_by
2.3.1.2. 멤버함수
def get_queryset(self):
def get_context_data(self, **kwargs):
class CustomListView(ListView):
model = MyModel
template_name = 'custom_list.html'
context_object_name = 'my_objects' #템플릿 엔진에서 사용
def get_queryset(self):
# 원하는 쿼리셋 반환
return MyModel.objects.filter(some_field__icontains='example')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['additional_data'] = 'Some additional data'
return context
def get_template_names(self):
if self.request.user.is_authenticated:
return ['custom_list_authenticated.html']
else:
return ['custom_list_anonymous.html']
def get(self, request, *args, **kwargs):
# 원하는 로직 수행 후 부모 클래스의 get 메소드 호출
response = super().get(request, *args, **kwargs)
# 추가적인 로직 수행
return response
https://ccbv.co.uk/projects/Django/5.0/django.views.generic.list/ListView/
2.4.1.1. 멤버변수
model
fields
form_name
template_name
success_url : '/xxx/yyy/zzz'하면 절대경로 & 'xxx/yyy/zzz' 하면 상대경로
2.4.1.2. 멤버함수
class ArticleCreateView(CreateView):
model = Article
form_class = ArticleCreationForm
template_name = 'blog/article_create.html'
success_url = '/blog'
def form_valid(self, form):
instance = form.save(commit=False)
instance.author = self.request.user
instance.save()
return super().form_valid(form)
cf) forms.py
class ArticleCreationForm(forms.ModelForm):
class Meta:
model = Article
fields=['title', 'content']
2.5.1.1. 멤버변수
model
template_name
context_object_name : 지정하지 않으면 None
: https://github.com/django/django/blob/main/django/views/generic/detail.py#L8
2.5.1.2. 멤버함수
2.6.1.1. 멤버변수
model
template_name
success_url
2.6.1.2. 멤버함수
def get_success_url(self)
예시
def get_success_url(self):
return reverse('blog:article-detial', kwargs={ 'pk': self.object.pk })
self.object.pk가 1이라면 결과는 다음과 같다.
/blog/article-detail/1/