Bagaimana menyebarkan berkas tetap

Melayani berkas statis di produksi

The basic outline of putting static files into production consists of two steps: run the collectstatic command when static files change, then arrange for the collected static files directory (STATIC_ROOT) to be moved to the static file server and served. Depending the staticfiles STORAGES alias, files may need to be moved to a new location manually or the post_process method of the Storage class might take care of that.

As with all deployment tasks, the devil's in the details. Every production setup will be a bit different, so you'll need to adapt the basic outline to fit your needs. Below are a few common patterns that might help.

Melayani situs dan berkas-berkas tetap anda dari peladen sama

Jika anda ingin melayani berkas-berkas tetap anda dari peladen sama yang sudah melayani situs anda, pengolahan mungkin terlihat sesuatu seperti:

Anda mungkin akan ingin mengotomatisasi pengolahan ini, khususnya jika anda mendapatkan banyak peladen jaringan.

Melayani berkas statis dari sebuah server tersendiri

Most larger Django sites use a separate web server -- i.e., one that's not also running Django -- for serving static files. This server often runs a different type of web server -- faster but less full-featured. Some common choices are:

Konfigurasi peladen ini diluar cakupan dari dokumen ini; periksa setiap dokumentasi masing-masing peladen untuk perintah.

Sejak peladen berkas tetap anda tidak akan menjalankan Django, anda akan butuh merubah strategi penyebaran menjadi kelihatan sesuatu seperti:

  • Ketika berkas statis anda berubah, jalankan collectstatic secara lokal.
  • Dorong STATIC_ROOT lokal anda sampai pada peladen berkas tetap kedalam direktori yang sedang dilayani. rsync adalah sebuah pilihan umum untuk langkah ini sejak dia hanya butuh memindahkan bit ke berkas-berkas tetap yang telah berubah.

Melayani berkas statis dari layanan cloud atau CDN

Another common tactic is to serve static files from a cloud storage provider like Amazon's S3 and/or a CDN (content delivery network). This lets you ignore the problems of serving static files and can often make for faster-loading web pages (especially when using a CDN).

Ketika menggunakan layanan ini, alurkerja dasar akan kelihatan sedikit seperti diatas, kecuali bahwa daripada menggunakan rsync untuk memindahkan berkas-berkas tetap anda ke peladen anda akan butuh untuk memindahkan berkas-berkas tetap ke penyedia panyimpanan atau CDN.

There's any number of ways you might do this, but if the provider has an API, you can use a custom file storage backend to integrate the CDN with your Django project. If you've written or are using a 3rd party custom storage backend, you can tell collectstatic to use it by setting staticfiles in STORAGES.

Sebagai contoh, jika anda telah menulis sebuah backend penyimpanan S3 dalam myproject.storage.S3Storage anda dapat menggunakannya dengan:

STORAGES = {
    # ...
    "staticfiles": {"BACKEND": "myproject.storage.S3Storage"}
}

Once that's done, all you have to do is run collectstatic and your static files would be pushed through your storage package up to S3. If you later needed to switch to a different storage provider, you may only have to change staticfiles in the STORAGES setting.

Untuk rincian pada bagaimana anda menulis satu dari backend ini, lihat Bagaimana menulis kelas penyimpanan disesuaikan. Ada aplikasi pihak 3 tersedia yang menyediakan backend penyimpanan untuk banyak API penyimpanan berkas umum. Titik awal bagus adalah overview at djangopackages.org.

Changed in Django 4.2:

The STORAGES setting was added.

Pelajari lagi

Untuk rincian lengkap pada semua pengaturan, perintah, cetakan etiket, dan potongan-potongan lainnya dalam django.contrib.staticfiles, lihat the staticfiles reference.

Back to Top