Django static structure
I am trying to understand the static structure django 1.3 tries to pursue:
I have a Project with this structure:
Project
someapp
static
someapp
css
etcetera
models.py
views.py
urls.py
urls.py
manage.py
settings.py
Now I wish to overwrite the django admin.. So I have to set these settings in settings.py which I did like below (basepath is the shortcut path to the current directory):
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/home/media/media.lawrence.com/static/"
STATIC_ROOT = BASE_PATH+'/static/'
# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'
# URL prefix for admin static files -- CSS, JavaScript and images.
# Make sure to use a trailing slash.
# Examples: "http://foo.com/static/admin/", "/static/admin/".
ADMIN_MEDIA_PREFIX = '/static/admin/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, 开发者_开发知识库even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
If I use the manage.py command collectstatic, it collects all static files (including the admin files) in a directory 'static' as expected... (within the main project dir)
However it's content isn't served yet until I add that directory to the STATICFILES_DIRS tuple, however then I have to change the STATIC_ROOT directory setting because otherwise I'll get the error they cannot be the same...
I think I am overlooking the obvious because what I have to do to make it work seems redundant
For local development, try this structure
Project
Project (project directory with settings.py etc..)
stylesheets
someapp
static
base.css
With this in settings.py
:
import os
ROOT_PATH = os.path.dirname(__file__)
STATIC_ROOT = os.path.join(ROOT_PATH, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(ROOT_PATH, 'stylesheets'),
)
Run the local server with python manage.py runserver
and go to http://localhost:8000/static/base.css
You should see the stylesheet.
STATICFILES_DIRS is a setting you use to declare non app-specific static files live in your project. STATIC_ROOT is where the static files get placed when they are collected.
From django's docs:
"Your project will probably also have static assets that aren’t tied to a particular app. The STATICFILES_DIRS setting is a tuple of filesystem directories to check when loading static files. It’s a search path that is by default empty. See the STATICFILES_DIRS docs how to extend this list of additional paths."
"Set the STATIC_ROOT setting to point to the filesystem path you'd like your static files collected to when you use the collectstatic management command."
How about:
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
STATIC_ROOT,
)
精彩评论