Django Static Structure

I am trying to understand the static structure that django 1.3 is trying 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 want to overwrite the django admin. Therefore, I have to set these parameters in settings.py, which I liked 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 to collect, it collects all static files (including admin files) in the "static" directory, as expected ... (in the main project directory)

However, this content is not shown until I add this directory to the STATICFILES_DIRS tuple, but then I need to change the STATIC_ROOT directory parameter, because otherwise I will get an error, they cannot be the same ...

I think I'm ignoring the obvious, because what I have to do to make it work seems redundant

+8
python django
source share
3 answers

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'), ) 

Launch the local server using python manage.py runserver and go to http://localhost:8000/static/base.css

You should see a stylesheet.

+10
source share

STATICFILES_DIRS is a parameter that you use to declare application-independent static files in real time in your project. STATIC_ROOT is the place where static files are placed when they are collected.

From django docs :

“Your project will likely also have static assets that are tied to a specific application. The STATICFILES_DIRS parameter is a file system directory tuple to check when loading static files. Its search path is empty by default. See STATICFILES_DIRS documents for how to expand this list of additional paths. "

"Set the STATIC_ROOT parameter to specify the path to the file system to which you would like to collect your static files when you use the collection management command.

+4
source share

What 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, ) 
-5
source share

All Articles