Writing views¶
뷰 함수, 짧게 말해 뷰*는, 웹 요청을 받아 웹 응답을 반환하는 파이썬 함수입니다. 이 응답은 웹 페이지의 HTML 콘텐츠일 수도, 리다이렉트나 404 에러, XML 문서, 이미지… 혹은 정말 아무거나 될 수 있습니다. 뷰는 응답을 반환할 때 필요한 임의의 로직을 가지고 있습니다. 이 코드는 파이썬 경로 상에 존재하는 한 어디에나 있을 수 있습니다. 다른 요구사항은 없습니다. 다시 말해, 《마법》은 없습니다. 코드를 *어딘가에 놓기 위해, 일반적인 관습은 ``views.py``라는 파일을 프로젝트나 앱 디렉터리에 만들어 넣어놓는 것입니다.
A simple view¶
여기 현재 날짜와 시간을 HTML 문서로 반환하는 뷰가 있습니다:
from django.http import HttpResponse
import datetime
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
한 줄씩 이 코드를 밟아나가 봅시다.
첫 번째로,
django.http
모듈로부터HttpResponse
클래스를, 파이썬의datetime
라이브러리와 함께 임포트합니다.다음으로
current_datetime
라는 함수를 정의합니다. 이것이 뷰 함수입니다. 각각의 뷰 함수는HttpRequest
객체를 첫 인자로 가지며, 이 객체는 일반적으로 ``request``라는 이름입니다.뷰 함수의 이름은 중요하지 않습니다. Django가 인식하도록 만들기 위해 특정한 방법으로 이름지을 필요는 없습니다. 여기서는 ``current_datetime``이라 부를 것이고, 이 이름이 하는 일을 분명하게 표현하기 때문입니다.
뷰는 생성된 응답을 담고 있는
HttpResponse
객체를 반환합니다. 각 뷰 함수는HttpResponse
객체를 반환하는 역할을 합니다. (예외가 있지만, 나중에 알아보겠습니다)
Mapping URLs to views¶
따라서 이 뷰 함수는 현재 날짜와 시간을 포함한 HTML 페이지를 반환합니다. 특정한 URL에 이 뷰를 표시하고 싶다면, *URLconf*를 만들어야 합니다. 자세한 내용은 URL dispatcher 를 참조하세요.
에러 반환¶
Django provides help for returning HTTP error codes. There are subclasses of
HttpResponse
for a number of common HTTP status codes
other than 200 (which means 《OK》). You can find the full list of available
subclasses in the request/response
documentation. Return an instance of one of those subclasses instead of a
normal HttpResponse
in order to signify an error. For
example:
from django.http import HttpResponse, HttpResponseNotFound
def my_view(request):
# ...
if foo:
return HttpResponseNotFound('<h1>Page not found</h1>')
else:
return HttpResponse('<h1>Page was found</h1>')
There isn’t a specialized subclass for every possible HTTP response code,
since many of them aren’t going to be that common. However, as documented in
the HttpResponse
documentation, you can also pass the
HTTP status code into the constructor for HttpResponse
to create a return class for any status code you like. For example:
from django.http import HttpResponse
def my_view(request):
# ...
# Return a "created" (201) response code.
return HttpResponse(status=201)
Because 404 errors are by far the most common HTTP error, there’s an easier way to handle those errors.
The Http404
exception¶
-
class
django.http.
Http404
¶
When you return an error such as HttpResponseNotFound
,
you’re responsible for defining the HTML of the resulting error page:
return HttpResponseNotFound('<h1>Page not found</h1>')
For convenience, and because it’s a good idea to have a consistent 404 error page
across your site, Django provides an Http404
exception. If you raise
Http404
at any point in a view function, Django will catch it and return the
standard error page for your application, along with an HTTP error code 404.
예시 용례:
from django.http import Http404
from django.shortcuts import render
from polls.models import Poll
def detail(request, poll_id):
try:
p = Poll.objects.get(pk=poll_id)
except Poll.DoesNotExist:
raise Http404("Poll does not exist")
return render(request, 'polls/detail.html', {'poll': p})
In order to show customized HTML when Django returns a 404, you can create an
HTML template named 404.html
and place it in the top level of your
template tree. This template will then be served when DEBUG
is set
to False
.
When DEBUG
is True
, you can provide a message to Http404
and
it will appear in the standard 404 debug template. Use these messages for
debugging purposes; they generally aren’t suitable for use in a production 404
template.
Customizing error views¶
The default error views in Django should suffice for most Web applications, but can easily be overridden if you need any custom behavior. Specify the handlers as seen below in your URLconf (setting them anywhere else will have no effect).
The page_not_found()
view is overridden by
handler404
:
handler404 = 'mysite.views.my_custom_page_not_found_view'
The server_error()
view is overridden by
handler500
:
handler500 = 'mysite.views.my_custom_error_view'
The permission_denied()
view is overridden by
handler403
:
handler403 = 'mysite.views.my_custom_permission_denied_view'
The bad_request()
view is overridden by
handler400
:
handler400 = 'mysite.views.my_custom_bad_request_view'
더 보기
Use the CSRF_FAILURE_VIEW
setting to override the CSRF error
view.
Testing custom error views¶
To test the response of a custom error handler, raise the appropriate exception in a test view. For example:
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.test import SimpleTestCase, override_settings
from django.urls import path
def response_error_handler(request, exception=None):
return HttpResponse('Error handler content', status=403)
def permission_denied_view(request):
raise PermissionDenied
urlpatterns = [
path('403/', permission_denied_view),
]
handler403 = response_error_handler
# ROOT_URLCONF must specify the module that contains handler403 = ...
@override_settings(ROOT_URLCONF=__name__)
class CustomErrorHandlerTests(SimpleTestCase):
def test_handler_renders_template_response(self):
response = self.client.get('/403/')
# Make assertions on the response here. For example:
self.assertContains(response, 'Error handler content', status_code=403)
비동기 뷰¶
As well as being synchronous functions, views can also be asynchronous
(《async》) functions, normally defined using Python’s async def
syntax.
Django will automatically detect these and run them in an async context.
However, you will need to use an async server based on ASGI to get their
performance benefits.
Here’s an example of an async view:
import datetime
from django.http import HttpResponse
async def current_datetime(request):
now = datetime.datetime.now()
html = '<html><body>It is now %s.</body></html>' % now
return HttpResponse(html)
You can read more about Django’s async support, and how to best use async views, in 비동기 지원.