【问题标题】:How to add static in my django如何在我的 django 中添加静态
【发布时间】:2012-12-07 11:47:02
【问题描述】:

我的文件夹结构如下:

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。 它显示

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

【问题讨论】:

  • 当你创建一个 django 项目时,它会自动设置。你删除了吗?

标签: django


【解决方案1】:

我认为你需要设置开发模式

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

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 2011-04-25
    • 1970-01-01
    • 2019-11-20
    • 2021-01-09
    • 2018-07-09
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    相关资源
    最近更新 更多