0

Referring to a comment-question asked in the answer of this question, let's say I have two static files from two different apps that both have the same name style.css:

   /foo
      /static
         /css
            /style.css
      /views.py
      /models.py
      /urls.py
   /bar
      /static
         /css
            /style.css
      /views.py
      /models.py
      /urls.py

And I manage.py collectstatic all the files into STATIC_ROOT, what happens to style.css? How is precedence established for which style.css to load? How does each app's templates know which style.css to load?

I guess what I'm really asking is: what is the django best practice procedure for having static files with the same name in the same project?

Community
  • 1
  • 1
Escher
  • 5,418
  • 12
  • 54
  • 101

1 Answers1

0

This is in the tutorial docs of django. Link here. Namespace your static files. Use the {app_name}/static/{app_name} convention like so.

/foo
  /static
    /foo # add this subdirectory
      /css
        /style.css
  /views.py
  /models.py
  /urls.py
/bar
  /static
    /bar # add this subdirectory
      /css
        /style.css
  /views.py
  /models.py
  /urls.py

This way you can have identical filenames. You can now refer to these files as foo/css/style.css and bar/css/style.css in {% static %} tags. The URLs would then be {STATIC_ROOT}/foo/css/style.css and {STATIC_ROOT}/bar/css/style.css.

Wannabe Coder
  • 1,457
  • 12
  • 21