Δείτε επίσης
Για μια εισαγωγή στη χρήση του module django.contrib.staticfiles
, δείτε στο άρθρο Διαχειρίζοντας τα static files (πχ εικόνες, JavaScript, CSS κλπ).
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 on
STATICFILES_STORAGE
, 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.
Αν θέλετε να εξυπηρετήσετε τα static files σας από τον ίδιο server ο οποίος εξυπηρετεί ήδη το site σας, η διαδικασία θα μοιάζει κάπως έτσι:
collectstatic
για να αντιγραφτούν όλα τα static files μέσα στο φάκελο STATIC_ROOT
.STATIC_ROOT
στο URL STATIC_URL
. Για παράδειγμα, δείτε πως μπορείτε να το κάνετε αυτό με τον Apache και το mod_wsgi.You’ll probably want to automate this process, especially if you’ve got multiple web servers.
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:
Η παραμετροποίηση αυτών των servers είναι εκτός του πεδίου εφαρμογής του παρόντος άρθρου. Συμβουλευτείτε το εγχειρίδιο καθενός από τους παραπάνω servers για λεπτομέρειες και οδηγίες.
Εφόσον ο static file server δεν θα τρέχει το Django, θα χρειαστεί να αλλάξετε τη στρατηγική του deployment για να μοιάζει κάπως έτσι:
collectstatic
τοπικά στον υπολογιστή σας.STATIC_ROOT
στον static file server μέσα στο φάκελο όπου εξυπηρετείται. Η εντολή rsync είναι μια κοινή λύση για αυτό το βήμα καθώς δεν αντιγράφει κάθε φορά όλο το φάκελο, παρά μόνο τις αλλαγές που έγιναν.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).
Όταν χρησιμοποιείτε αυτές τις υπηρεσίες, η βασική διαδικασία θα μοιάζει λίγο-πολύ με την παραπάνω, εκτός του ότι αντί να χρησιμοποιείτε την εντολή rsync
για να μεταφέρετε τα static files στον server, θα χρειαστεί να μεταφέρετε τα static files στον πάροχο αποθήκευσης αρχείων ή στο 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_STORAGE
to the storage engine.
Για παράδειγμα, αν έχετε γράψει ένα σύστημα αποθήκευσης S3 στο αρχείο myproject.storage.S3Storage
, μπορείτε να το χρησιμοποιήσετε ως εξής:
STATICFILES_STORAGE = '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 your STATICFILES_STORAGE
setting.
For details on how you’d write one of these backends, see How to write a custom storage class. There are 3rd party apps available that provide storage backends for many common file storage APIs. A good starting point is the overview at djangopackages.org.
Για πλήρεις λεπτομέρειες πάνω σε όλες τις ρυθμίσεις, εντολές, template tags και άλλα κομμάτια που περιέχονται στο django.contrib.staticfiles
, δείτε στο άρθρο αναφορά στα staticfiles.
Μαρ 08, 2023