A view function, or view for short, is simply a Python function that takes a
Web request and returns a Web response. This response can be the HTML contents
of a Web page, or a redirect, or a 404 error, or an XML document, or an image .
. . or anything, really. The view itself contains whatever arbitrary logic is
necessary to return that response. This code can live anywhere you want, as long
as it's on your Python path. There's no other requirement--no "magic," so to
speak. For the sake of putting the code somewhere, the convention is to
put views in a file called views.py
, placed in your project or
application directory.
Disini adalah tampilan yang mengembalikan data dan waktu saat ini, sebagai sebuah dokumen 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)
Mari kita melangkah melalui kode ini satu baris pada saat bersamaan:
Pertama, kami impor kelas HttpResponse
dari modul django.http
, bersama dengan pustaka datetime
Python.
Next, we define a function called current_datetime
. This is the view
function. Each view function takes an HttpRequest
object as its first parameter, which is typically named request
.
Note that the name of the view function doesn't matter; it doesn't have to
be named in a certain way in order for Django to recognize it. 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.)
Zona Waktu Django
Django includes a TIME_ZONE
setting that defaults to
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 Pengirim URL 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)
Karena kesalahan 404 sejauh ini paling umum kesalahan HTTP, ada sebuah cara paling mudah untuk menangani kesalahan-kesalahan tersebut.
Http404
¶django.http.
Http404
¶Ketika anda mengembalikan sebuah kesalahan seperti HttpResponseNotFound
, anda bertanggung jawab untuk menentukan HTML dari menghasilkan halaman kesalahan:
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.
Contoh penggunaan:
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})
Untuk menampilkan HTML disesuaikan ketika Django mengembalikan sebuah 404, anda dapat membuat sebuah cetakan HTML bernama 404.html
dan menempatkan itu dalam tingkat atas dari pohon cetakan anda. Cetakan anda kemudian akan dilayani ketika DEBUG
disetel menjadi False
.
Ketika DEBUG
adalah True
, anda dapat menyediakan sebauh pesan pada Http404
dan itu akan muncul dalam cetakan mencari kesalahan 404 standar. Gunakan pesan-pesan ini untuk tujuan mencari kesalahan; mereka umumnya tidak cocok untuk digunakan dalam produksi cetakan 404.
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).
Tampilan page_not_found()
ditimpa oleh handler404
:
handler404 = 'mysite.views.my_custom_page_not_found_view'
Tampilan server_error()
ditimpa oleh handler500
:
handler500 = 'mysite.views.my_custom_error_view'
Tampilan permission_denied()
ditimpa oleh handler403
:
handler403 = 'mysite.views.my_custom_permission_denied_view'
Tampilan bad_request()
ditimpa oleh handler400
:
handler400 = 'mysite.views.my_custom_bad_request_view'
lihat juga
Gunakan pengaturan CSRF_FAILURE_VIEW
utnuk menimpa tampilan kesalahan CSRF.
Des 02, 2017