【问题标题】:Django deploy : Should I use bootstrap3 CDN?Django 部署:我应该使用 bootstrap3 CDN 吗?
【发布时间】:2018-02-18 03:13:13
【问题描述】:

我有我的网站。它使用 Django 制作并部署到 Heroku。
我使用了bootstrap3。在部署到 heroku 之前,当我在 localhost:8000 测试网站时,bootstrap3 运行良好。
我使用 bootstrap3 源文件,不是 CDN

下面是我的源代码树。

/home/<path>/multichat$ tree
.
├── manage.py
├── static
│   ├── bootstrap-3.3.7-dist
│   │   ├── css
│   │   │   ├── bootstrap-theme.css
│   │   │   ├── bootstrap-theme.css.map
│   │   │   ├── bootstrap-theme.min.css
│   │   │   ├── bootstrap-theme.min.css.map
│   │   │   ├── bootstrap.css
│   │   │   ├── bootstrap.css.map
│   │   │   ├── bootstrap.min.css
│   │   │   └── bootstrap.min.css.map
│   │   ├── fonts
│   │   │   ├── glyphicons-halflings-regular.eot
│   │   │   ├── glyphicons-halflings-regular.svg
│   │   │   ├── glyphicons-halflings-regular.ttf
│   │   │   ├── glyphicons-halflings-regular.woff
│   │   │   └── glyphicons-halflings-regular.woff2
│   │   └── js
│   │       ├── bootstrap.js
│   │       ├── bootstrap.min.js
│   │       └── npm.js
│   ├── css
│   │   └── base.css
│   └── js
│       └── jquery-1.12.2.min.js
├── staticfiles
│   └── default_profile.png
└── templates
    ├── 404.html
    ├── 500.html
    ├── base.html
    ├── home.html
    └── index.html

我所有的 html 文件都扩展了 templates/base.html
为了应用 bootstrap3,我在 base.html 中这样编码。

<!-- bootstrap -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}" type="text/css" />

<!-- Optional theme -->
<link rel="stylesheet" href="{% static 'bootstrap-3.3.7-dist/css/bootstrap-theme.min.css' %}" type="text/css" />

<!-- Latest compiled and minified JavaScript -->
<script src="{% static 'bootstrap-3.3.7-dist/js/bootstrap.min.js' %}" type="text/javascript"></script>
<!-- bootstrap end -->

管理静态文件是我的settings.py。

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT=os.path.join(BASE_DIR, 'staticfiles')

部署到 heroku 后,我访问了我的网站。未应用 Bootstrap3。我修改了base.html。

我删除了 href="{% static 'bootstrap-3.3.7-dist/css/bootstrap.min.css' %}" 并添加了 CDN
已应用 bootstrap3。

但是我想使用 bootstrap3 下载文件。我不想使用 CDN。
生产环境使用bootstrap3,怎么办?

【问题讨论】:

  • “我不想使用 CDN” - 为什么不呢?

标签: python django twitter-bootstrap heroku


【解决方案1】:

虽然它应该双向工作。但我会推荐使用 Bootstrap CDN

但如果它在那里不起作用,那么只需在生产服务器上运行python manage.py collectstatic 后检查静态文件的存储位置并进行相应的更改。

如果下载的引导程序现在不工作,这意味着你的 css 文件都不能工作,因为你的静态文件由 python manage.py collectstatic 存储在其他地方。因此不是下载的引导程序或cdn的问题。

【讨论】: