视图函数或简称视图只是一个Python函数,它接受Web请求并返回Web响应。 此响应可以是网页的HTML内容,重定向,404错误,XML文档或图像。
. . or anything, really. 视图本身包含返回该响应所需的任意逻辑。 This code can live anywhere you want, as long
as it’s on your Python path. 没有其他要求 - 没有“魔术”,可以这么说。 为了将代码放在某处,惯例是将视图放在名为views.py
的文件中,放在项目或应用程序目录中。
这是一个返回当前日期和时间的视图,作为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
,以及Python的datetime
库。
接下来,我们定义一个名为current_datetime
的函数。 这是一个视图函数。 每个视图函数都将HttpRequest
对象作为其第一个参数,通常命名为request
。
请注意,视图函数的名称无关紧要;它不必以某种方式命名,以便Django识别它。 We’re
calling it current_datetime
here, because that name clearly indicates
what it does.
The view returns an HttpResponse
object that
contains the generated response. Each view function is responsible for
returning an HttpResponse
object. (There are
exceptions, but we’ll get to those later.)
Django’s Time Zone
Django包含一个默认的 TIME_ZONE
设置,它的值为 America/Chicago
. This probably isn’t where you live, so you might want
to change it in your settings file.
So, to recap, this view function returns an HTML page that includes the current date and time. To display this view at a particular URL, you’ll need to create a URLconf; see URL dispatcher for instructions.
Returning HTTP error codes in Django is easy. 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. Just 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.
Http404
exception¶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.
Example usage:
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.
The default error views in Django should suffice for most Web applications, but can easily be overridden if you need any custom behavior. Simply 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'
See also
Use the CSRF_FAILURE_VIEW
setting to override the CSRF error
view.
Jan 17, 2018