So, my team and I are new to Django development. We’ve been trying to design our project structure over the last couple weeks, and we’ve done a lot of reading to try and get a grasp of the best practice of doing this.
However, we are still struggling to get our heads around the best way to store our templates/static files that are generic to all applications.
Let me detail our structure we’ve implemented so far:
root-folder/
app-1/
static/
app-1/
css/
js/
img/
templates/
base.html
base-app-1.html **extends base.html
app-2/
static/
app-2/
css
js
img
templates/
base.html **extends base.html at 'project-name' level
base-app-2.html **extends base.html
project-name/
collectstatic/
templates/
base.html
static/
base/
css
js
img
The one thing that bugs me with this structure is that our top level templates reside inside the ‘project-name’ level. I would like to see this folder at the same level as our root level static folder.
However, I don’t want to move the root level static folder content into ‘project-name/collectstatic’ because I don’t want to have files inside that folder being version controlled.
We realise that there is no definitive answer to this problem in the Django world. But, are we on the right lines? Have we misunderstood something?
A couple ideas we’ve got are:
- To have an app dedicated to top level templates and htdocs called something along the lines of ‘general’ or ‘generic’.
- To move the ‘project-name/templates’ dir to the root level along side our ‘static’ folder there.
- To stop talking about this problem and go with the structure drawn out above.
In Django it’s preferable to rely on staticfiles’
AppDirectoriesFinderfinder and the template’s ‘app_directories.Loader` loader classes for managing your static files and templates.That means that you’ll have some sort of
project.core,project.baseorproject.commonsapplication that you’ll populate with base templates and static resources that are reused throughout your project.I’m assuming your problem with the
project-name/collectstatic/folder is that it’s your definedSTATIC_ROOT, on a path that’s under version control. Ideally, you’ll move your static and media root outside of your project’s sources. The simplest solution is to move it next to your project’s top-level package, in the standard project layout for distributing Python packages, or to your Web server’s preconfigured document root.In either way, this should move your locally generated static resources outside of your project’s Python packages.