Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I structure my static app files in Django?

Tags:

django

I'm looking for advise on how to structure up my Django project. This is my current structure at the moment:

project_name
  - deploy
  - app_name
    -- migrations
    -- templates
    -- templatetags
    -- __init__.py
    -- admin.py
    -- context_processors.py
    -- defaults.py
    -- models.py
    -- tests.py
    -- views.py
  - static
    -- admin
    -- app_name
      --- css
      --- fonts
      --- img
      --- js
      --- tinymce
  - templates
  - project_name
  - __init__.py
  - fabfile.py
  - manage.py

I wonder if it's better to have all the static files in my app folder or should they be like it is at the moment in the static folder and app_name?

Or is it another better way to structure my static files?

like image 960
Inte L Avatar asked Jan 06 '16 15:01

Inte L


2 Answers

Put them in a directory named "static" under your app's directory. That's where django.contrib.staticfiles.finders.AppDirectoriesFinder looks for them by default.

Inside the static/ directory, create a directory with your app's name.

So on deployment, when you run ./manage.py collectstatic, Django will copy all the files over to the directory defined in settings.STATIC_ROOT and each app will have its files inside its own sub-directory.

project_name
- deploy
- app_name
  - migrations
  ...
  - static
    - app_name
      - css
      - fonts
      - img
      - js
      - tinymce
  - templates
    - app_name
      - item.html
      - list.html
- project_name
- __init__.py
- manage.py

Also, depending on the number of fonts, CSS, and JS files you have, I'd simply put them inside app_name/static/app_name/ and not create another directory level js/ and css/. "Flat is better than nested", as they say.

like image 67
C14L Avatar answered Sep 30 '22 00:09

C14L


If all your point is about static files, by rule of thumb:

  • put them in your application if they are specific to your application
  • put them in a dependency (say, another app you should need to be installed in your django project, although that concept is not enforced by Django, it is enforced by Python package management system (say, setuptools) if you plan to release your django app) if your assets are global to many django apps you have. You should also release (if it is the case) and install the dependency in your django project (otherwise files will not be reachable).
  • put them in a global static directory for your project, if they are only specific to the project.

They are only tips, but the most-practiced ones. You can place them where you want as long as the path is reachable by Django staticfiles loaders.

like image 28
Luis Masuelli Avatar answered Sep 30 '22 01:09

Luis Masuelli