Django静态结构 [英] Django static structure

查看:146
本文介绍了Django静态结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图了解静态结构django 1.3尝试追求:



我有一个这样的结构的项目:

 项目
someapp
static
someapp
css
etcetera
models.py
views.py
urls.py
urls.py
manage.py
settings.py

现在我想覆盖django管理员..所以我必须在settings.py中设置这些设置,我以下所做的(basepath是当前目录的快捷方式):

 #应该收集目录静态文件的绝对路径。 
#不要把任何东西放在这个目录下;在应用程序的static /子目录和STATICFILES_DIRS中存储静态文件
#。
#示例:/home/media/media.lawrence.com/static/
STATIC_ROOT = BASE_PATH +'/ static /'

#静态文件的URL前缀。
#示例:http://media.lawrence.com/static/
STATIC_URL ='/ static /'

#admin静态文件的URL前缀 - CSS ,JavaScript和图像。
#确保使用尾部斜线。
#示例:http://foo.com/static/admin/,/ static / admin /。
ADMIN_MEDIA_PREFIX ='/ static / admin /'

#静态文件的附加位置
STATICFILES_DIRS =(
#将字符串放在这里,如/ home / html /静态或C:/ www / django / static
#即使在Windows上也可以使用正斜杠
#不要忘记使用绝对路径,而不是相对路径

如果我使用manage.py命令collectstatic,它会收集所有静态文件(包括管理文件)在一个目录'static'如预期的...(在主项目目录中)



但是,直到我将该目录添加到STATICFILES_DIRS内容才被提供元组,然而,我必须更改STATIC_ROOT目录设置,否则我会得到错误,他们不能一样...



我想我忽略了明显的因为我需要做的是让它工作似乎是多余的

解决方案

对于本地开发,尝试这个结构

 项目
项目(具有settings.py等的项目目录)
样式表
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, '样式表'),

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



你应该看到样式表。


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.

这篇关于Django静态结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆