I’m trying my first proper webdev project and i’m learning the django framework.
I came here to ask about the cleanest way to use “static files”, like the external CSS i’m referring to in one of my html templates. I tried reading through the official documentation on the subject but found it a little bit confusing as a beginner, i then tried googling but i noticed that most of the guides or stackoverflow answers differed slightly and i realised i needed a better understanding. Bit cheeky to ask but, could someone explain and summarise the process to me?
For reference, here is my project folder hierarchy. At the moment i’m trying to get the template base.html to use the sylesheet at CSS/base.css:

Also one of the things that keeps throwing me off is the use of absolute filepaths. So far i’ve managed to get away with just using relative filepaths, which makes more sense to me as the aim is to develop on the django test server then transfer it all onto my own server when i have one. (note: perhaps it’s because i have no idea how complicated that transfer process is that i don’t understand why absolute filepaths are prefered). What’s the issue with using relative filepaths?
I realise that this has sort of become two questions, which is against the rules, but i really think both could be answered together and if i understood one it would probably aid my understanding of the other. Any help would be much appreciated.
I’m going to assume you’re using Django 1.3 or higher as that allows you to use
staticfiles(docs) for static files. Here’s the basic idea: each Django website consists of multiple apps (directories) that contain all the files needed for a particular part of the website (i.e., amodels.py,views.py, atemplatesdirectory and also astaticdirectory). Thisstaticdirectory contains the JavaScript and CSS files (as well as other static files for that app.When deploying the website, you run the
collectstaticcommand to gather all the static files into one directory. From that directory you then serve the static files to visitors of your site (i.e, Apache or a different web server can do that for you).So the general structure of your web project could be:
You’ll notice that the name of the Django app is repeated in the subdirectories of the
staticandtemplatesdirectories. This is necessary because it keeps the files apart when they are moved to a generalstaticdirectory. In the above example, theSTATIC_ROOTis the top-levelstaticdirectory and into this directory all your static files will be copied.It’s definitely worth reading the documentation of
staticfilesas this project layout allows you to reuse Django apps in other projects with ease. For example, you can imagine a website that looks like this:The apps
blogandphotoscan now easily be reused in other websites if desired.