Django static vs. user uploaded files Django static vs. user uploaded files django django

Django static vs. user uploaded files


1

Static files are everything that your HTML templates require in order to display properly - CSS, JavaScript, images, Flash files etc. They are treated differently as there is nothing special that Django has to do with them. (With your views.py, it has to run the Python code in them, with templates, it needs to render any template tags you've used etc - with static assets, they just need to be served "as is" to the user)

The collectstatic command will look in the "static" subdirectory of each of your apps, as well as any additional directories you have defined in the STATICFILES_DIRS setting. This means your web server (Apache, Nginx etc) only needs to serve up that one directory to make all of your assets available to your users, rather than each individual app's "static" directory.

2

User uploaded files won't be collected by the collectstatic command - and can be kept separately. You don't want to mix up your own CSS/JS/images etc with them.

They already go in a single directory that can be served by your websever, and don't need to be routinely collected at all.


What exactly is a static file?

Static files are the files that you have created in your project and need to be served directly to to the browser (usually JavaScript, CSS, and images).
Your Python files like views.py are not directly sent to the browser, they do some processing to create an HTML file that will be sent to the browser.

What does "serving static files" mean?

Serving static files is sending the file in response to a web request, which is done by a web server.

why do I even need "collectstatic" anyway?

collectstatic uses the STATICFILES_DIRS variable in your settings.py to find all of your static files and puts them in one place. You have to do this in your production server because that folder should have a different security setting than your project files. You don't want user to be able to download your model.py for example.
collectstatic can also involve minification and obfuscation using something like django-pipeline.

Do I need a program running in the background that constantly runs "collectstatic"?

No, media files are a different story. These are file that users upload to your system (as opposed to the static files that YOU create). These files are usually saved to the file system on your server and a record is created in your database with the address to the file an some meta data defined in your model. models.FileField has a upload_to attribute that controls where these files should be stored relative to the MEDIA_ROOT variable in your settings.py.

These files should be separate from your project files on the server or even on some other server, so that they remain in place when you update your site.


Most of answers are already given in docs:

  1. https://docs.djangoproject.com/en/1.8/ref/contrib/staticfiles/ - how django find static files and more

  2. https://docs.djangoproject.com/en/1.8/howto/static-files/ - how static files should be served in dev

  3. https://docs.djangoproject.com/en/1.8/howto/static-files/deployment/ - how static files should be served in pro + some intro to automatic collectstatic via fabric.

In a few words: static is those files that are responsible for the appearance of your site not for its structure and info representation(css, js, img, videos & etc.).

In dev you put all static files in a static and it is automatically served by dev server.

When you are on pro, static files should be stored in a folder staticfiles/static outside your project's dir. Media in staticfiles/media (you may give some other name to those dirs). That should be done for two reasons: in terms of security and for all your static to be served by separate web-server. You might want to automatically run collectstatic on every change you make - try fabric for it. Users upload all files already in your media folder - so you do not need to run collectstatic for it.