Django static files and file paths in settings.py

I am trying to create my first webdev project and I am learning the django framework.

I came here to ask about the cleanest way to use "static files" such as external CSS, which I refer to in one of my html templates. I tried to read the official documentation on this topic , but found it a little confusing as a beginner, then I tried googling, but I noticed that most of the answers from the manual or stackoverflow were slightly different, and I realized that I needed a better understanding. It’s a little cheeky to ask, but can someone explain and generalize this process for me?

For reference, here is the hierarchy of project folders. I'm currently trying to get a template base.htmlfor using sylesheet in CSS/base.css: enter image description here

Also, one of the things that throw me away is using absolute file paths. So far, I managed to get away just using relative file paths, which makes more sense to me, since the goal is to develop on the django test server and then transfer it to my server when I have it. (note: perhaps because I have no idea how complicated this transfer process is that I don’t understand why absolute file paths are preferred). What is the problem with using relative file paths?

, , , , , , , , . .

+5
3

, Django 1.3 , staticfiles (docs) . : Django (), , - (.. models.py, views.py, a templates, static). static JavaScript CSS ( .

- collectstatic . (, Apache - ).

, - :

  • manage.py
  • /models.py
  • /views.py
  • ///CSS/base.css
  • ///JS/base.js
  • ///base.html
  • settings.py
  • urls.py

, Django static templates. , , static. STATIC_ROOT - static, .

, staticfiles, Django . , -, :

  • /models.py
  • ///overview.html
  • ///post.html
  • /urls.py
  • /views.py
  • /models.py
  • ///overview.html
  • ///photo.html
  • /urls.py
  • /views.py
  • manage.py
  • settings.py
  • urls.py

blog photos , .

+2

settings.py, Django drchrono.

import os
import django
DJANGO_ROOT = os.path.dirname(os.path.realpath(django.__file__))
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

SITE_ROOT

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3', 
            'NAME': os.path.join(SITE_ROOT,'data.db'),                      

sqlite3,

            'USER': '',
            'PASSWORD': '',
            'HOST': '',                     
            'PORT': '',                     
        }
    }


STATIC_ROOT = SITE_ROOT
STATIC_URL = '/static/'

.

STATICFILES_DIRS = (
    os.path.join(SITE_ROOT,'static'),
       )

TEMPLATE_DIRS = (
    os.path.join(SITE_ROOT,'templates'),

)

 {% load static %} --at beginning
<link href="{% static "css/bootstrap.min.css"%}" rel="stylesheet" media="screen">

, , "css/bootstrap.min.css"

+4

, @Simeon Visser, - , , . sinister_user_name reddit, , , , - :


, .

:

static_url, django .

<link rel="stylesheet" href="{{ STATIC_URL }}css/bootstrap.min.css">

URL, . static_url /static/ , ? URL- -, , . , .

, STATIC_ROOT , , -, . , manage.py collectstatic, . STATIC_ROOT, .

, , , - , python. , , - . , , python.

, :

manage.py
website/settings.py
app/models.py
app/views.py
templates/base.html
static/js/jquery.js
static/css/bootstrap.css

, , url .

: , settings.py, , , , :( 1.4 :

import os
import django
import manage

DJANGO_ROOT = os.path.dirname(os.path.realpath(django.__file__))
SITE_ROOT = os.path.dirname(os.path.realpath(manage.__file__))

, ( 1.4 1.3), SITE_ROOT . , . :

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(SITE_ROOT, 'static'),
)

, . .

+1

All Articles