【问题标题】:Linking static files directory in Django在Django中链接静态文件目录
【发布时间】:2013-10-22 20:56:54
【问题描述】:

我有多个静态文件目录。每个应用程序都有自己的静态文件目录,以使其模块化。如何访问所有应用程序的静态文件目录。最初,我只将所有静态文件放在一个文件夹下。现在我将静态文件保存在应用程序中,然后想从应用程序内部访问它。如何更改我的 settings.py 文件以访问静态目录。

这是我的目录结构。

|-- assets                      // static folder named as 'assets'
|   |-- css
|   |   |-- bootstrap.css
|   |   |-- bootstrap.min.css
|   |   |-- bootstrap-responsive.css
|   |   |-- bootstrap-responsive.min.css
|   |   `-- login.css
|   |-- img
|   |   |-- glyphicons-halflings.png
|   |   `-- glyphicons-halflings-white.png
|   `-- js
|       |-- bootstrap.js
|       |-- bootstrap.min.js
|       `-- jquery-1.9.1.min.js

|-- initial                    // My Project Name
|   |-- __init__.py
|   |-- __init__.pyc
|   |-- settings.py
|   |-- settings.pyc
|   |-- urls.py
|   |-- urls.pyc
|   |-- wsgi.py
|   `-- wsgi.pyc
|-- manage.py
|-- models.py
|-- modules                   //apps folder named as 'modules'
|   |-- dashboard
|   |   |-- __init__.py
|   |   |-- __init__.pyc
|   |   |-- models.py
|   |   |-- models.pyc
|   |   |-- static            // static folder inside the dashboard app.
|   |   |   |-- css
|   |   |   |-- img
|   |   |   `-- js
|   |   |       `-- dashboard.js
|   |   |-- templates            // template folder inside the dashboard app.
|   |   |   `-- dashboard
|   |   |       `-- dashboard.html
|   |   |-- tests.py
|   |   |-- urls.py
|   |   |-- urls.pyc
|   |   |-- views.py
|   |   `-- views.pyc
|   |-- login            // login app
|   |   |-- forms.py
|   |   |-- forms.pyc
|   |   |-- __init__.py
|   |   |-- __init__.pyc
|   |   |-- models.py
|   |   |-- models.pyc
|   |   |-- static
|   |   |   |-- css
|   |   |   |   `-- login.css
|   |   |   |-- img
|   |   |   `-- js
|   |   |-- templates
|   |   |   |-- auth
|   |   |   |   |-- login.html
|   |   |   |   |-- logout.html
|   |   |   |   `-- register.html
|   |   |   `-- registration
|   |   |       `-- login.html
|   |   |-- tests.py
|   |   |-- urls.py
|   |   |-- urls.pyc
|   |   |-- views.py
|   |   `-- views.pyc
|  
`-- templates              // templates folder for base templates.
    |-- base1.html
    |-- base2.html
    `-- registration
        `-- login.html

这是我的 settings.py 文件,当时所有静态文件都在一个文件夹下。

MEDIA_ROOT = os.path.normpath( os.path.join(os.path.dirname(__file__), '../assets/'))

MEDIA_URL = ''

STATIC_ROOT = ''

STATIC_URL = '/assets/'

这是我的 settings.py 文件,当时所有静态文件都在各自的模块/应用程序下。

MEDIA_ROOT = (
    os.path.normpath( os.path.join(os.path.dirname(__file__), '../assets/')),
    os.path.normpath( os.path.join(os.path.dirname(__file__), '../modules/dashboard/static/')),
    os.path.normpath( os.path.join(os.path.dirname(__file__), '../modules/login/static/')),
    )

MEDIA_URL = ''

STATIC_ROOT = ''

STATIC_URL = '/assets/'

【问题讨论】:

标签: python css django twitter-bootstrap


【解决方案1】:

您应该遵循以下步骤(来自文档):https://docs.djangoproject.com/en/dev/howto/static-files/

最重要的部分是:

将您的静态文件存储在应用中名为 static 的文件夹中。例如 my_app/static/my_app/myimage.jpg。

因此,将名称从 assets 更改为 static

【讨论】:

  • 为什么要改成static?有什么相关性吗?
  • @user1162512 因为这就是django.contrib.staticfiles 的工作方式。它将在子文件夹/static 中搜索每个应用程序中的静态文件。它将省略其他文件夹。
  • 更具体地说,这就是django.contrib.staticfiles.finders.AppDirectoriesFinder 的工作方式。如果您想做一些自定义的事情,您可以在设置中创建自己的STATICFILES_FINDER。两个默认查找器将搜索显式位置 (STATICFILES_DIRS),或在每个已安装应用程序中的 /static/ 下搜索。还要记住,url 路径将是 STATIC_URL + /static 之后的文件路径。
  • 虽然我已经获得了所需的 o/p,但我可以从两者之间省略 myapp 文件夹。我尝试将其删除,但效果不一样。有什么具体原因吗?
猜你喜欢
  • 2019-06-04
  • 2013-04-06
  • 2015-06-14
  • 2015-02-05
  • 2019-05-28
  • 1970-01-01
  • 2013-01-10
  • 2020-12-11
  • 2015-08-28
相关资源
最近更新 更多