Where does the static files reside

39 views
Skip to first unread message

Gary Roach

unread,
Nov 7, 2015, 6:56:33 PM11/7/15
to django...@googlegroups.com
Which is the correct file structure for static files

Putting the static files under the individual project as shown with
"home' below:
├── archive
│ ├── archive
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── settings.py
│ │ ├── settings.pyc
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── home
│ │ ├── admin.py
│ │ ├── admin.pyc
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── models.py
│ │ ├── models.pyc
│ │ ├── static
│ │ │ ├── home.css
│ │ │ ├── images
│ │ │ └── __init__.py
│ │ ├── tests.py
│ │ └── views.py
│ ├── __init__.py
│ ├── manage.py
│ └── templates
│ ├── admin
│ │ ├── base_site.html
│ │ └── __init__.py
│ ├── home
│ │ ├── index2.html
│ │ ├── index.html
│ │ └── __init__.py
│ └── __init__.py

or should they be under the project "root" as shown below.

├── archive
│ ├── archive
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── settings.py
│ │ ├── settings.pyc
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── home
│ ├── __init__.py
│ │ ├── admin.py
│ │ ├── admin.pyc
│ │ ├── __init__.py
│ │ ├── __init__.pyc
│ │ ├── models.py
│ │ ├── models.pyc
│ │ ├── tests.py
│ │ └── views.py
│ ├── static
│ │ └── __init__.py
| | |___home
│ │ ├── home.css
│ │ ├── images
| | django.gif
│ ├── manage.py
│ └── templates
│ ├── admin
│ │ ├── base_site.html
│ │ └── __init__.py
│ ├── home
│ │ ├── index2.html
│ │ ├── index.html
│ │ └── __init__.py
│ └── __init__.py

The second way seems to be the more logical but the tutorial set things
up as shown in the first. Either way, I have not been able to get either
one working (serving up static files). Since there are dozens of queries
about serving static files and dozens of ways shown - none of which
worked for me and many of which conflicted with each other - all I want
is the answer to this one question and nothing else. After that I will
work with the next step.

I'm using Debian 8 (jessie) os. This complicates things a bit because
Debian takes care of a lot of the setup automatically that other Linux
versions don't. I'm using Django 1.8 and Python 3.4

Please use the KISS principle on this one.

Thanks in advance

Gary R.


René Fleschenberg

unread,
Nov 7, 2015, 7:29:55 PM11/7/15
to django...@googlegroups.com
Hi Gary,

> ├── archive
> │ ├── archive
> │ │ ├── __init__.py
> │ │ ├── settings.py
> │ │ ├── urls.py
> │ │ └── wsgi.py
> │ ├── home
> │ │ ├── __init__.py
> │ │ ├── static
> │ │ │ ├── home.css
> │ ├── manage.py

In your first example, "home" is an app (not a project) that has some static
files. "archive" is your project.

Your "home" app doesn't follow the recommended filesystem structure, though.
You should do this:

archive/
home/
__init__.py
static/
home/
home.css

Note that there is no need to have an __init__.py file in any of your static
dirs.

In your second example:

> ├── archive
> │ ├── archive
> │ │ ├── __init__.py
> │ │ ├── settings.py
> │ │ ├── urls.py
> │ │ └── wsgi.py
> │ ├── home
> │ ├── static
> | | |___home
> │ │ ├── home.css
> │ │ ├── images

you have what I would call a "global" static files directory. Files from
this directory will only be found if you configure that directory in your
settings:

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)

This is because that directory is not part of any app that is listed in
INSTALLED_APPS.

BASE_DIR is the top-level "archive" directory, by the way (the first
"archive" in the tree).

I hope this helps.


Because this gets asked so often, I am trying to to sump it up here:

https://fleschenberg.net/django-staticfiles/

I'd love to hear your feedback about this article, i.e. if it clarifies
things for you, or how to improve it.

--
René Fleschenberg

Gary Roach

unread,
Nov 8, 2015, 10:44:07 AM11/8/15
to django...@googlegroups.com
On 11/07/2015 04:29 PM, René Fleschenberg wrote:
> Hi Gary,
Thanks for the tips. I read your 'In a nutshell" . Good work. Now,
getting into setup philosophy, it occurs to me that it would be nice to
have global static files for such things as a style.css file since the
file should set the style of your whole web site. I suppose that one
could put a copy of the file under each apps static folder but change
control would be horrible. Could I set up both a global static folder
and then have a static folder under each app? Being rather new to this
rats nest (great framework all the same) I'm probably skating on rather
thin ice here. Be kind.

As to your comment on the first tree, I think I created the problem
while cutting and pasting. The actual project works - mostly.

A comment on "6 An alternative approach". This actually my case. When
you say that the app listed first takes precedence.

Looking forward to your comments.

Gary R

Andreas Kuhne

unread,
Nov 8, 2015, 11:00:58 AM11/8/15
to django...@googlegroups.com
Hi Gary,

You can certainly setup a global static files location. All you need to do is add a path to STATICFILES_DIRS in your settings file. However, that is not really necessary. All files that are in a static directory in one app are accessible in other apps. You can create an app that contains css and js files needed for the other apps and concentrate them in one app. The only thing you need to think about is if you want to make sure that the apps are completely unconnected.

Regards,

Andréas



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/563F6DA5.1050909%40verizon.net.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages