如何在我的django中添加静态

时间:2012-12-07 11:47:02

标签: django

我的文件夹结构如下:

mysite_new/
          manage.py
          mysite/
                 __init__.py
                 urls.py
                 setting.py
                 wsgi.py
                 templates/
                          default.html
                 static/
                        admin/
                              img/
                                  logo.png

          ticket/
                __init__.py
                models.py
                view.py
                urls.py
                ......

在setting.py中,我设置了

STATIC_URL = '/static/'
STATIC_ROOT = '' 
STATICFILES_DIRS = (

)

并在ticket / urls.py中设置

from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT )

default.html中

<html>
<head>
<img src="{{ STATIC_URL }}admin/img/logo.jpg" />

</head>
<body>
</body>
</html>  

然后我尝试访问包含default.html的页面,未显示logo.jpg。 它显示enter image description here

所以静态文件夹可能没有正确链接到我的项目,我的问题是谁可以告诉我如何实现这个?  我应该在setting.py(STATIC_ROOT,STATIC_URL,STATICFILES_DIRS),urls.py中设置什么以及如何修改default.html。请详细说明详细步骤。感谢

1 个答案:

答案 0 :(得分:2)

我认为对于开发模式,您需要设置

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

和urls.py

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

要收集静止,您需要设置STATIC_ROOT,如:

STATIC_ROOT = os.path.join(PROJECT_ROOT, '..', 'static')
相关问题