Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 1000
reedvoid Avatar asked Sep 27 '15 06:09

reedvoid


1 Answers

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.

like image 86
Oli Avatar answered Oct 05 '22 01:10

Oli