# dokumentasi Django 2.2.9.dev20191202104323

# Tampilan Siap pakai

Several of Django's built-in views are documented in Menulis tampilan as well as elsewhere in the documentation.

## Melayani berkas di pengembangan

`static.``serve`(request, path, document_root, show_indexes=False)

There may be files other than your project's static assets that, for convenience, you'd like to have Django serve for you in local development. The `serve()` view can be used to serve any directory you give it. (This view is not hardened for production use and should be used only as a development aid; you should serve these files in production using a real front-end web server).

The most likely example is user-uploaded content in `MEDIA_ROOT`. `django.contrib.staticfiles` is intended for static assets and has no built-in handling for user-uploaded files, but you can have Django serve your `MEDIA_ROOT` by appending something like this to your URLconf:

    from django.conf import settings
    from django.urls import re_path
    from django.views.static import serve

    # ... the rest of your URLconf goes here ...

    if settings.DEBUG:
        urlpatterns += [
            re_path(r'^media/(?P<path>.*)$', serve, {
                'document_root': settings.MEDIA_ROOT,
            }),
        ]

Note, the snippet assumes your `MEDIA_URL` has a value of `'/media/'`. This will call the `serve()` view, passing in the path from the URLconf and the (required) `document_root` parameter.

Since it can become a bit cumbersome to define this URL pattern, Django ships with a small URL helper function `static()` that takes as parameters the prefix such as `MEDIA_URL` and a dotted path to a view, such as `'django.views.static.serve'`. Any other function parameter will be transparently passed to the view.

## Tampilan kesalahan

Django comes with a few views by default for handling HTTP errors. To override these with your own custom views, see Menyesuaikan tampilan kesalahan.

### Tampilan 404 (halaman tidak ditemukan)

`defaults.``page_not_found`(request, exception, template_name='404.html')

When you raise `Http404` from within a view, Django loads a special view devoted to handling 404 errors. By default, it's the view `django.views.defaults.page_not_found()`, which either produces a very simple "Not Found" message or loads and renders the template `404.html` if you created it in your root template directory.

The default 404 view will pass two variables to the template: `request_path`, which is the URL that resulted in the error, and `exception`, which is a useful representation of the exception that triggered the view (e.g. containing any message passed to a specific `Http404` instance).

Tiga hal untuk dicatat tentang tampilan 404:

- The 404 view is also called if Django doesn't find a match after checking every regular expression in the URLconf.
- The 404 view is passed a `RequestContext` and will have access to variables supplied by your template context processors (e.g. `MEDIA_URL`).
- If `DEBUG` is set to `True` (in your settings module), then your 404 view will never be used, and your URLconf will be displayed instead, with some debug information.

### Tampilan 500 (kesalahan peladen)

`defaults.``server_error`(request, template_name='500.html')

Similarly, Django executes special-case behavior in the case of runtime errors in view code. If a view results in an exception, Django will, by default, call the view `django.views.defaults.server_error`, which either produces a very simple "Server Error" message or loads and renders the template `500.html` if you created it in your root template directory.

The default 500 view passes no variables to the `500.html` template and is rendered with an empty `Context` to lessen the chance of additional errors.

If `DEBUG` is set to `True` (in your settings module), then your 500 view will never be used, and the traceback will be displayed instead, with some debug information.

### Tampilan 403 (Terlarang HTTP)

`defaults.``permission_denied`(request, exception, template_name='403.html')

In the same vein as the 404 and 500 views, Django has a view to handle 403 Forbidden errors. If a view results in a 403 exception then Django will, by default, call the view `django.views.defaults.permission_denied`.

This view loads and renders the template `403.html` in your root template directory, or if this file does not exist, instead serves the text "403 Forbidden", as per RFC 7231#section-6.5.3 (the HTTP 1.1 Specification). The template context contains `exception`, which is the string representation of the exception that triggered the view.

`django.views.defaults.permission_denied` dipicu oleh sebuah pengecualian `PermissionDenied`. Untuk menyangkal akses dalam sebuah tampilan anda dapat menggunakan kode seperti ini:

    from django.core.exceptions import PermissionDenied

    def edit(request, pk):
        if not request.user.is_staff:
            raise PermissionDenied
        # ...

### Tampilan (permintaan buruk) 400

`defaults.``bad_request`(request, exception, template_name='400.html')

Ketika sebuah `SuspiciousOperation` dimunculkan di Django, dia mungkin ditangani oleh komponen dari Django (sebagai contoh mengatur kembali data sesi). Jika tidak secara khusus ditangani, Django akan memperimbangkan permintaan saat ini 'bad request' daripada kesalahan peladen.

`django.views.defaults.bad_request`, jika tidak sangat mirip pada tampilan `server_error`, tetapi mengembalikan dengan kode keadaan 400 yang kondisi kesalahan adalah hasil dari pengerjaan klien. Secara awal, tidak ada terhubung pada pengecualian yang memicu tampilan dilewatkan ke konteks cetakan, sebagai pesan pengecualian mungkin mengandung informasi sensitif seperti jalur sistem berkas.

Tampilan `bad_request` juga hanya digunakan ketika `DEBUG` adalah `False`.