Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static folders structure in Django 1.4?

This is the new project structure (from the Django 1.4 release notes).

myproject
|-- manage.py
|-- myproject
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py
`-- polls
    |-- __init__.py
    |-- models.py
    |-- tests.py
    `-- views.py

What I am not sure about is whether I should point STATIC_ROOT to

myproject/myproject/static/ (together with settings.py, urls.py...)

OR

The top-level directory myproject/static (next to myproject, myapp1, myapp2)?

like image 505
Sahat Yalkabov Avatar asked Apr 16 '12 04:04

Sahat Yalkabov


3 Answers

STATIC_ROOT is not related to Python importing, so it totally depends on you. Normally, myproject/static/, thus os.path.join(PROJECT_ROOT, 'static/') in settings, is easier.

update as San4ez suggested, and notes inside settings.py

# 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/"  

Better to put static files of the poll app into poll/static/, according to your structure.

like image 157
okm Avatar answered Sep 23 '22 22:09

okm


I agree with @okm that myproject/static/ is a good place for static, but also you can store images, css and js inside your app in myproject/polls/media. Than you have to configure django.contrib.staticfiles app and copy static from media to STATIC_ROOT with command

python manage.py collectstatic

The advantage of this approach that this allows you to spread your app with static and your app can be used freely in other projects

like image 25
San4ez Avatar answered Sep 23 '22 22:09

San4ez


STATIC_ROOT is just a file path where the staticfiles contrib app will collect and deposit all static files. It is a location to collect items, that's all. The key thing is that this location is temporary storage and is used mainly when packaging your app for deployment.

The staticfiles app searches for items to collect from any directory called static in any apps that are listed in INSTALLED_APPS and in addition any extra file path locations listed in STATICFILES_DIRS.

For my projects I create a deploy directory in which I create a www folder that I use for static files, and various other files used only when deploying. This directory is at the top level of the project.

You can point the variable to any location to which your user has write permissions, it doesn't need to be in the project directory.

like image 43
Burhan Khalid Avatar answered Sep 19 '22 22:09

Burhan Khalid