---
title: "Aplikasi staticfiles"
version: 1.10
locale: id
source: https://docs.djangoproject.com/id/1.10/ref/contrib/staticfiles/
canonical: https://djangodocs.dev/id/1.10/ref/contrib/staticfiles/
---
# Aplikasi `staticfiles`

`django.contrib.staticfiles` menyimpan berkas-berkas tetap dari setiap dari aplikasi anda (dan setiap tempat lain anda tentukan) kedalam tempat tunggal yang dapat dengan mudah dilayani di produksi.

> **See also**
>
> Untuk sebuah perkenalaln pada aplikasi berkas-berkas tetap dan beberapa penggunaan contoh, lihat [Mengelola berkas statis (sebagai contoh gambar, JavaScript, CSS)](/id/1.10/howto/static-files/). Untuk panduan dalam menyebarkan berkas-berkas tetap, lihat [menyebarkan berkas tetap](/id/1.10/howto/static-files/deployment/).

## Pengaturan

Lihat [staticfiles settings](/id/1.10/ref/settings/#settings-staticfiles) untuk rincian pada pengaturan berikut:

- [`STATIC_ROOT`](/id/1.10/ref/settings/#std-setting-STATIC_ROOT)
- [`STATIC_URL`](/id/1.10/ref/settings/#std-setting-STATIC_URL)
- [`STATICFILES_DIRS`](/id/1.10/ref/settings/#std-setting-STATICFILES_DIRS)
- [`STATICFILES_STORAGE`](/id/1.10/ref/settings/#std-setting-STATICFILES_STORAGE)
- [`STATICFILES_FINDERS`](/id/1.10/ref/settings/#std-setting-STATICFILES_FINDERS)

## Pengelolaan perintah

`django.contrib.staticfiles` membuka tiga perintah pengelolaan.

### `collectstatic`

#### `django-admin collectstatic`

Kumpulkan berkas-berkas tetap kedalam [`STATIC_ROOT`](/id/1.10/ref/settings/#std-setting-STATIC_ROOT).

Duplicate file names are by default resolved in a similar way to how template
resolution works: the file that is first found in one of the specified
locations will be used. If you're confused, the [`findstatic`](#django-admin-findstatic) command
can help show you which files are found.

On subsequent `collectstatic` runs (if `STATIC_ROOT` isn't empty), files
are copied only if they have a modified timestamp greater than the timestamp of
the file in `STATIC_ROOT`. Therefore if you remove an application from
[`INSTALLED_APPS`](/id/1.10/ref/settings/#std-setting-INSTALLED_APPS), it's a good idea to use the [`collectstatic
--clear`](#cmdoption-collectstatic-clear) option in order to remove stale static files.

Files are searched by using the [`enabled finders`](/id/1.10/ref/settings/#std-setting-STATICFILES_FINDERS). The default is to look in all locations defined in
[`STATICFILES_DIRS`](/id/1.10/ref/settings/#std-setting-STATICFILES_DIRS) and in the `'static'` directory of apps
specified by the [`INSTALLED_APPS`](/id/1.10/ref/settings/#std-setting-INSTALLED_APPS) setting.

The [`collectstatic`](#django-admin-collectstatic) management command calls the
[`post_process()`](#django.contrib.staticfiles.storage.StaticFilesStorage.post_process)
method of the [`STATICFILES_STORAGE`](/id/1.10/ref/settings/#std-setting-STATICFILES_STORAGE) after each run and passes
a list of paths that have been found by the management command. It also
receives all command line options of [`collectstatic`](#django-admin-collectstatic). This is used
by the [`CachedStaticFilesStorage`](#django.contrib.staticfiles.storage.CachedStaticFilesStorage)
by default.

By default, collected files receive permissions from
[`FILE_UPLOAD_PERMISSIONS`](/id/1.10/ref/settings/#std-setting-FILE_UPLOAD_PERMISSIONS) and collected directories receive permissions
from [`FILE_UPLOAD_DIRECTORY_PERMISSIONS`](/id/1.10/ref/settings/#std-setting-FILE_UPLOAD_DIRECTORY_PERMISSIONS). If you would like different
permissions for these files and/or directories, you can subclass either of the
[static files storage classes](#staticfiles-storages) and specify the
`file_permissions_mode` and/or `directory_permissions_mode` parameters,
respectively. For example:

```
from django.contrib.staticfiles import storage

class MyStaticFilesStorage(storage.StaticFilesStorage):
    def __init__(self, *args, **kwargs):
        kwargs['file_permissions_mode'] = 0o640
        kwargs['directory_permissions_mode'] = 0o760
        super(MyStaticFilesStorage, self).__init__(*args, **kwargs)
```

Kemudian setel pengaturan [`STATICFILES_STORAGE`](/id/1.10/ref/settings/#std-setting-STATICFILES_STORAGE) menjadi `'path.to.MyStaticFilesStorage'`.

Beberapa pilihan umum digunakan adalah:

#### `--noinput, --no-input`

Do NOT prompt the user for input of any kind.

> **Changed in Django 1.9**
>
> Nama lain `--no-input` telah ditambahkan.

#### `--ignore PATTERN, -i PATTERN`

Abaikan berkas-berkas atau direktori cocok dengan pola gaya-global ini. Gunakan beberapa kali untuk mengabaikan lebih.

#### `--dry-run, -n`

Lakukan apapun kecuali merubah sistem berkas.

#### `--clear, -c`

Bersihkan berkas-berkas yang ada sebelum mencoba menyalin atau menaut berkas asli.

#### `--link, -l`

Buat sebuah tautan simbolus pada setiap berkas daripada menyalin.

#### `--no-post-process`

Jangan memanggil metode [`post_process()`](#django.contrib.staticfiles.storage.StaticFilesStorage.post_process) dari backend penyimpanan [`STATICFILES_STORAGE`](/id/1.10/ref/settings/#std-setting-STATICFILES_STORAGE) yang sudah dikonfigurasi.

#### `--no-default-ignore`

Jangan abaikan pola-gaya-blobal pribadi umum `'CVS'`, `'.*'` dan `'*~'`.

Untuk daftar penuh dari pilihan, mengacu pada perintah bantuan sendiri dengan menjalankan:

```console
$ python manage.py collectstatic --help
```

#### Menyesuaikan daftar pola diabaikan

> **New in Django 1.10**

The default ignored pattern list, `['CVS', '.*', '*~']`, can be customized in
a more persistent way than providing the `--ignore` command option at each
`collectstatic` invocation. Provide a custom [`AppConfig`](/id/1.10/ref/applications/#django.apps.AppConfig)
class, override the `ignore_patterns` attribute of this class and replace
`'django.contrib.staticfiles'` with that class path in your
[`INSTALLED_APPS`](/id/1.10/ref/settings/#std-setting-INSTALLED_APPS) setting:

```python
from django.contrib.staticfiles.apps import StaticFilesConfig

class MyStaticFilesConfig(StaticFilesConfig):
    ignore_patterns = [...]  # your custom ignore list
```

### `findstatic`

#### `django-admin findstatic staticfile [staticfile ...]`

Mencari untuk satu atau lebih jalur relatif dengan diadakan penemu.

Sebagai contoh:

```console
$ python manage.py findstatic css/base.css admin/js/core.js
Found 'css/base.css' here:
  /home/special.polls.com/core/static/css/base.css
  /home/polls.com/core/static/css/base.css
Found 'admin/js/core.js' here:
  /home/polls.com/src/django/contrib/admin/media/js/core.js
```

#### `findstatic --first`

By default, all matching locations are found. To only return the first match
for each relative path, use the `--first` option:

```console
$ python manage.py findstatic css/base.css --first
Found 'css/base.css' here:
  /home/special.polls.com/core/static/css/base.css
```

This is a debugging aid; it'll show you exactly which static file will be
collected for a given path.

Dengan mengatur bendera `--verbosity` menjadi 0, anda dapat menekan keluaran tambahan dan hanya mendapatkan nama-nama jalur:

```console
$ python manage.py findstatic css/base.css --verbosity 0
/home/special.polls.com/core/static/css/base.css
/home/polls.com/core/static/css/base.css
```

Di pihak lain, dengan mengatur bendera `--verbosity` menjadi 2, anda dapat mendapatkan semua direktori yang telah dicari:

```console
$ python manage.py findstatic css/base.css --verbosity 2
Found 'css/base.css' here:
  /home/special.polls.com/core/static/css/base.css
  /home/polls.com/core/static/css/base.css
Looking in the following locations:
  /home/special.polls.com/core/static
  /home/polls.com/core/static
  /some/other/path/static
```

### `runserver`

#### `django-admin runserver [addrport]`

Overrides the core [`runserver`](/id/1.10/ref/django-admin/#django-admin-runserver) command if the `staticfiles` app
is [`installed`](/id/1.10/ref/settings/#std-setting-INSTALLED_APPS) and adds automatic serving of static
files and the following new options.

#### `--nostatic`

Use the `--nostatic` option to disable serving of static files with the
[staticfiles](/id/1.10/ref/contrib/staticfiles/) app entirely. This option is
only available if the [staticfiles](/id/1.10/ref/contrib/staticfiles/) app is
in your project's [`INSTALLED_APPS`](/id/1.10/ref/settings/#std-setting-INSTALLED_APPS) setting.

Contoh penggunaan:

```console
django-admin runserver --nostatic
```

#### `--insecure`

Use the `--insecure` option to force serving of static files with the
[staticfiles](/id/1.10/ref/contrib/staticfiles/) app even if the [`DEBUG`](/id/1.10/ref/settings/#std-setting-DEBUG)
setting is `False`. By using this you acknowledge the fact that it's
**grossly inefficient** and probably **insecure**. This is only intended for
local development, should **never be used in production** and is only
available if the [staticfiles](/id/1.10/ref/contrib/staticfiles/) app is
in your project's [`INSTALLED_APPS`](/id/1.10/ref/settings/#std-setting-INSTALLED_APPS) setting. [`runserver`](/id/1.10/ref/django-admin/#django-admin-runserver)
`--insecure` doesn't work with
[`CachedStaticFilesStorage`](#django.contrib.staticfiles.storage.CachedStaticFilesStorage).

Contoh penggunaan:

```console
django-admin runserver --insecure
```

## Penyimpanan

### `StaticFilesStorage`

#### `class storage.StaticFilesStorage`

A subclass of the [`FileSystemStorage`](/id/1.10/ref/files/storage/#django.core.files.storage.FileSystemStorage)
storage backend that uses the [`STATIC_ROOT`](/id/1.10/ref/settings/#std-setting-STATIC_ROOT) setting as the base
file system location and the [`STATIC_URL`](/id/1.10/ref/settings/#std-setting-STATIC_URL) setting respectively
as the base URL.

#### `storage.StaticFilesStorage.post_process(paths, **options)`

This method is called by the [`collectstatic`](#django-admin-collectstatic) management command
after each run and gets passed the local storages and paths of found
files as a dictionary, as well as the command line options.

The [`CachedStaticFilesStorage`](#django.contrib.staticfiles.storage.CachedStaticFilesStorage)
uses this behind the scenes to replace the paths with their hashed
counterparts and update the cache appropriately.

### `ManifestStaticFilesStorage`

#### `class storage.ManifestStaticFilesStorage`

A subclass of the [`StaticFilesStorage`](#django.contrib.staticfiles.storage.StaticFilesStorage)
storage backend which stores the file names it handles by appending the MD5
hash of the file's content to the filename. For example, the file
`css/styles.css` would also be saved as `css/styles.55e7cbb9ba48.css`.

The purpose of this storage is to keep serving the old files in case some
pages still refer to those files, e.g. because they are cached by you or
a 3rd party proxy server. Additionally, it's very helpful if you want to
apply [far future Expires headers](https://developer.yahoo.com/performance/rules.html#expires) to the deployed files to speed up the
load time for subsequent page visits.

The storage backend automatically replaces the paths found in the saved
files matching other saved files with the path of the cached copy (using
the [`post_process()`](#django.contrib.staticfiles.storage.StaticFilesStorage.post_process)
method). The regular expressions used to find those paths
(`django.contrib.staticfiles.storage.HashedFilesMixin.patterns`)
by default covers the [@import](http://www.w3.org/TR/CSS2/cascade.html#at-import) rule and [url()](http://www.w3.org/TR/CSS2/syndata.html#uri) statement of [Cascading
Style Sheets](http://www.w3.org/Style/CSS/). For example, the `'css/styles.css'` file with the
content

```css+django
@import url("../admin/css/base.css");
```

would be replaced by calling the [`url()`](/id/1.10/ref/files/storage/#django.core.files.storage.Storage.url)
method of the `ManifestStaticFilesStorage` storage backend, ultimately
saving a `'css/styles.55e7cbb9ba48.css'` file with the following
content:

```css+django
@import url("../admin/css/base.27e20196a850.css");
```

To enable the `ManifestStaticFilesStorage` you have to make sure the
following requirements are met:

- the [`STATICFILES_STORAGE`](/id/1.10/ref/settings/#std-setting-STATICFILES_STORAGE) setting is set to
  `'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'`
- Pengaturan [`DEBUG`](/id/1.10/ref/settings/#std-setting-DEBUG) disetel ke `False`
- Anda telah mengumpulkan semua berkas tetap dengan menggunakan perintah pengelolaan [`collectstatic`](#django-admin-collectstatic)

> **Changed in Django 1.10**
>
> In older versions, you also had to use
> `{% load static from staticfiles %}` in your template. The
> [`static`](/id/1.10/ref/templates/builtins/#std-templatetag-static) template tag (`{% load static %}`) now uses
> [`django.contrib.staticfiles`](#module-django.contrib.staticfiles) if it's installed.

Since creating the MD5 hash can be a performance burden to your website
during runtime, `staticfiles` will automatically store the mapping with
hashed names for all processed files in a file called `staticfiles.json`.
This happens once when you run the [`collectstatic`](#django-admin-collectstatic) management
command.

Due to the requirement of running [`collectstatic`](#django-admin-collectstatic), this storage
typically shouldn't be used when running tests as `collectstatic` isn't run
as part of the normal test setup. During testing, ensure that the
[`STATICFILES_STORAGE`](/id/1.10/ref/settings/#std-setting-STATICFILES_STORAGE) setting is set to something else like
`'django.contrib.staticfiles.storage.StaticFilesStorage'` (the default).

#### `storage.ManifestStaticFilesStorage.file_hash(name, content=None)`

The method that is used when creating the hashed name of a file.
Needs to return a hash for the given file name and content.
By default it calculates a MD5 hash from the content's chunks as
mentioned above. Feel free to override this method to use your own
hashing algorithm.

### `CachedStaticFilesStorage`

#### `class storage.CachedStaticFilesStorage`

`CachedStaticFilesStorage` is a similar class like the
[`ManifestStaticFilesStorage`](#django.contrib.staticfiles.storage.ManifestStaticFilesStorage) class
but uses Django's [caching framework](/id/1.10/topics/cache/) for storing the
hashed names of processed files instead of a static manifest file called
`staticfiles.json`. This is mostly useful for situations in which you don't
have access to the file system.

If you want to override certain options of the cache backend the storage uses,
simply specify a custom entry in the [`CACHES`](/id/1.10/ref/settings/#std-setting-CACHES) setting named
`'staticfiles'`. It falls back to using the `'default'` cache backend.

## Finders Module

`staticfiles` finders has a `searched_locations` attribute which is a list
of directory paths in which the finders searched. Example usage:

```console
from django.contrib.staticfiles import finders

result = finders.find('css/base.css')
searched_locations = finders.searched_locations
```

## Other Helpers

There are a few other helpers outside of the
[`staticfiles`](#module-django.contrib.staticfiles) app to work with static
files:

- The [`django.template.context_processors.static()`](/id/1.10/ref/templates/api/#django.template.context_processors.static) context processor
  which adds [`STATIC_URL`](/id/1.10/ref/settings/#std-setting-STATIC_URL) to every template context rendered
  with [`RequestContext`](/id/1.10/ref/templates/api/#django.template.RequestContext) contexts.
- The builtin template tag [`static`](/id/1.10/ref/templates/builtins/#std-templatetag-static) which takes a path and urljoins it
  with the static prefix [`STATIC_URL`](/id/1.10/ref/settings/#std-setting-STATIC_URL). If
  `django.contrib.staticfiles` is installed, the tag uses the `url()`
  method of the [`STATICFILES_STORAGE`](/id/1.10/ref/settings/#std-setting-STATICFILES_STORAGE) instead.
- The builtin template tag [`get_static_prefix`](/id/1.10/ref/templates/builtins/#std-templatetag-get_static_prefix) which populates a
  template variable with the static prefix [`STATIC_URL`](/id/1.10/ref/settings/#std-setting-STATIC_URL) to be
  used as a variable or directly.
- The similar template tag [`get_media_prefix`](/id/1.10/ref/templates/builtins/#std-templatetag-get_media_prefix) which works like
  [`get_static_prefix`](/id/1.10/ref/templates/builtins/#std-templatetag-get_static_prefix) but uses [`MEDIA_URL`](/id/1.10/ref/settings/#std-setting-MEDIA_URL).

### Static file development view

The static files tools are mostly designed to help with getting static files
successfully deployed into production. This usually means a separate,
dedicated static file server, which is a lot of overhead to mess with when
developing locally. Thus, the `staticfiles` app ships with a
**quick and dirty helper view** that you can use to serve files locally in
development.

#### `views.serve(request, path)`

This view function serves static files in development.

> **Warning**
>
> This view will only work if [`DEBUG`](/id/1.10/ref/settings/#std-setting-DEBUG) is `True`.
>
> That's because this view is **grossly inefficient** and probably
> **insecure**. This is only intended for local development, and should
> **never be used in production**.

> **Note**
>
> Untuk menebak jenis isi berkas dilayani, tampilan ini bergantung pada modul [`mimetypes`](https://docs.python.org/3/library/mimetypes.html#module-mimetypes) dari pustaka standar Python, yang itu sendiri bergantung pada pokok berkas peta serambi. Jika anda menemukan bahwa tampilan ini tidak mengembalikanjenis isi yang sesuai untuk berkas-berkas tertent, itu kebanyakan sepertinya bahwa berkas-berkas peta serambi butuh diperbaharui. Ini dapat dicapai, sebagai contoh, dengan memasang atau memperbaharui paket `mailcap` pada penyaluran Red Hat, atau `mime-support` pada penyaluran Debian.

This view is automatically enabled by [`runserver`](/id/1.10/ref/django-admin/#django-admin-runserver) (with a
[`DEBUG`](/id/1.10/ref/settings/#std-setting-DEBUG) setting set to `True`). To use the view with a different
local development server, add the following snippet to the end of your
primary URL configuration:

```python
from django.conf import settings
from django.contrib.staticfiles import views

if settings.DEBUG:
    urlpatterns += [
        url(r'^static/(?P<path>.*)$', views.serve),
    ]
```

Note, the beginning of the pattern (`r'^static/'`) should be your
[`STATIC_URL`](/id/1.10/ref/settings/#std-setting-STATIC_URL) setting.

Since this is a bit finicky, there's also a helper function that'll do this for
you:

#### `urls.staticfiles_urlpatterns()`

This will return the proper URL pattern for serving static files to your
already defined pattern list. Use it like this:

```python
from django.contrib.staticfiles.urls import staticfiles_urlpatterns

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

urlpatterns += staticfiles_urlpatterns()
```

This will inspect your [`STATIC_URL`](/id/1.10/ref/settings/#std-setting-STATIC_URL) setting and wire up the view
to serve static files accordingly. Don't forget to set the
[`STATICFILES_DIRS`](/id/1.10/ref/settings/#std-setting-STATICFILES_DIRS) setting appropriately to let
`django.contrib.staticfiles` know where to look for files in addition to
files in app directories.

> **Warning**
>
> This helper function will only work if [`DEBUG`](/id/1.10/ref/settings/#std-setting-DEBUG) is `True`
> and your [`STATIC_URL`](/id/1.10/ref/settings/#std-setting-STATIC_URL) setting is neither empty nor a full
> URL such as `http://static.example.com/`.
>
> That's because this view is **grossly inefficient** and probably
> **insecure**. This is only intended for local development, and should
> **never be used in production**.

### Specialized test case to support 'live testing'

#### `class testing.StaticLiveServerTestCase`

This unittest TestCase subclass extends [`django.test.LiveServerTestCase`](/id/1.10/topics/testing/tools/#django.test.LiveServerTestCase).

Just like its parent, you can use it to write tests that involve running the
code under test and consuming it with testing tools through HTTP (e.g. Selenium,
PhantomJS, etc.), because of which it's needed that the static assets are also
published.

But given the fact that it makes use of the
[`django.contrib.staticfiles.views.serve()`](#django.contrib.staticfiles.views.serve) view described above, it can
transparently overlay at test execution-time the assets provided by the
`staticfiles` finders. This means you don't need to run
[`collectstatic`](#django-admin-collectstatic) before or as a part of your tests setup.
