Django静态结构

2024-05-20 03:42:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图理解django 1.3试图追求的静态结构:

我有一个这样的项目:

Project
   someapp
      static
          someapp
             css
             etcetera
      models.py
      views.py
      urls.py
   urls.py
   manage.py
   settings.py

现在我想覆盖django管理。。因此,我必须在settings.py中设置这些设置,如下所示(basepath是指向当前目录的快捷路径):

# 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.
)

如果我使用manage.py命令collectstatic,它将按预期收集“static”目录中的所有静态文件(包括管理文件)。。。(在主项目总监内)

但是,直到我将该目录添加到STATICFILES\u DIRS元组,它的内容才被提供,但是,然后我必须更改STATIC\u根目录设置,因为否则我将得到错误,它们不能相同。。。

我想我忽略了显而易见的事情,因为我要做的事情似乎是多余的


Tags: todjangoinpycomurladminuse
3条回答

static files是用于声明项目中存在非应用程序特定的静态文件的设置。静态根目录是收集静态文件时放置它们的位置。

来自django's docs

“你的项目可能也会有一些没有绑定到特定应用程序的静态资产。static files目录设置是加载静态文件时要检查的文件系统目录的元组。它是默认为空的搜索路径。请参阅STATICFILES\u DIRS文档如何扩展此附加路径列表。”

“将静态根设置设置为指向使用collectstatic管理命令时要收集的静态文件的文件系统路径。”

为了当地的发展,试试这个结构

Project
  Project (project directory with settings.py etc..)
       stylesheets
  someapp
  static
      base.css

把这个放在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'),
)

使用python manage.py runserver运行本地服务器并转到http://localhost:8000/static/base.css

您应该看到样式表。

怎么样:

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

相关问题 更多 >