Django and staticfiles questions

2019-08-21 09:50发布

The more I learn Django, the more I discover new things.

I read the official docs, stackoverflow and google but I still have doubts.

What's the correct/normal way to organize our static files?

I mean folders and settings.py

I have something like:

CURRENT_PATH = os.path.dirname(__file__)

STATIC_ROOT = os.path.join(CURRENT_PATH, 'static')

STATIC_URL = '/static/'

Ok, Im going to collect all my apps statics on ~/static/

I created a static/appname folder on every app and I put all my app's static there.

Also, I need a static folder to project-wide statics (what's the common name for it? Since I used /static/ for collected stuff and they cannot be equal).

So far so good, I have css like:

href="{{ STATIC_URL }}appname/styles.css"

and it works like charm.

But I think that when I deploy my app, I have to run 'collectstatic' so I put that '/static/' folder serving on Cherokee.

The question is... will that work? I tried commenting the AppDirectoryFinder and the _DIRS one and that doesn't work on local (Having the static stuff collected, I mean, the css on /static/ and in the other folders too).

Is just better to have one static folder on root for all the project? And copy the admin css to that folder (AKA manually collectstatic).

The projects I see on github/bitbucket are ready to be deployed, I need to know the steps to go from dev to deploy.

Thanks!

2条回答
该账号已被封号
2楼-- · 2019-08-21 10:29

I'll break this down as I use the django static app

url at which your static media will be served STATIC_URL = '/static/' this is used for 2 things {{STATIC_URL}} in your templates and the static file url and for hosting your static files in django (DEVELOPMENT ONLY)

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

The location at which your files reside on the server

STATIC_ROOT = '/var/www/website/static'

this is used when you run collectstatic and is where your webserver should be looking

your file finder definition

STATICFILES_FINDERS = ( 
                       'django.contrib.staticfiles.finders.FileSystemFinder',
                       'django.contrib.staticfiles.finders.AppDirectoriesFinder',
                       )

I've used the default from django you can of course use more but here is the crux of what you are looking to know

'django.contrib.staticfiles.finders.AppDirectoriesFinder'

will find any folder named "static" that is inside an installed app

'django.contrib.staticfiles.finders.FileSystemFinder'

will tell django to look at STATICFILES_DIRS (this is your project wide static files)

which should be defined as a tuple

STATICFILES_DIRS = ( 
                    join( CURRENT_PATH, 'static' ),
                    )

where 'static' is whatever you want and you can add in as many other folders to monitor as you wish.

the sub directories you place inside each app ie:app/static/appname are not necessary but will ensure that files of the same name inside different apps don't overwrite files from other apps or your root static folders

all of this was taken from my own experience and https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/

查看更多
Lonely孤独者°
3楼-- · 2019-08-21 10:44

Also, I need a static folder to project-wide statics (what's the common name for it? Since I used /static/ for collected stuff and they cannot be equal).

Are you sure? I'm pretty sure I'm using the same folder to collect my static files and to hold my project-wide static files. Not sure if that's not recommended pracice, but it works for me.

Note that this is just on the deployment side; my codebase just has the project static files.

查看更多
登录 后发表回答