reedvoid
reedvoid

Reputation: 1253

Django static vs. user uploaded files

After reading a ton of documentation, I still don't have a good grasp on what exactly counts as a static vs. a user uploaded file... or what exactly a static file is even.

1. Static files

Django describes static files as things like "images, javascript, css". OK, makes sense. But all those .py files and .html template files etc... they aren't static?? They don't "change" while the site is running. What exactly is a static file? What does "serving static files" mean? How is that different than "serving" a "views.py" file or a "home.html" file?

On top of that, why do I even need "collectstatic" anyway? Why do all my static files need to be in one place? I suppose if you're using a separate web server to "serve" them that makes sense... still, not sure what exactly that even means.

Also, where does "collectstatic" go to find all the supposed static files anyway? Does it go through all my code and see where I'm accessing image / javascript files?

2. User uploaded files

Say I just take this static thing at face value, what happens if a user uploads an image? Do I need a program running in the background that constantly runs "collectstatic"?

Django describes media files as a way to manage user-uploaded content. Honestly I haven't tried this (will do so right now), but still this represents a question: if it's so important to put all the "static" files in one place, why is it suddenly OK not to do that for user-uploaded files?

Are the files inside this "MEDIA_ROOT" directory not collected by "collectstatic"? What if I just put all my images / javascript / css into this media folder? Then nothing is collected? Anything wrong with that?

Thanks for any insights anyone can shed on this.

Upvotes: 8

Views: 2601

Answers (3)

chem1st
chem1st

Reputation: 1634

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.

Upvotes: 1

nima
nima

Reputation: 6733

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.

Upvotes: 2

Oli
Oli

Reputation: 1003

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.

Upvotes: 3

Related Questions