Bootstrap Django 模板无法正常工作

Bootstrap template not working correctly Django

我有一个 Bootstrap 模板,我正尝试将其与 Django 一起使用,并且我已将 bootstrap 资源与我正在尝试的 html 文件放在模板目录中显示。我的问题是,当试图显示 html 文件时,它只在浏览器 window 中显示为文本,所以这会让我相信 Bootstrap 资源没有正确显示使用过。

这是我尝试使用的模板:http://startbootstrap.com/template-overviews/stylish-portfolio/

我的模板目录如下所示:

css
font-awesome
fonts
img
js
index.html

我知道它使用 CDN 链接,但是当我下载 zip 时资源与模板一起,所以我把它留在了那里。我是 Bootstrap 的新手,也是 Django 的新手,所以请放轻松,如果我的术语有问题,请纠正我。谢谢

您应该将它放在 static 文件夹中并配置您的设置。否则,如果您仍想将其放在 template 中,请更改您的设置,但这并不常见。模板文件夹应仅包含 html 个文件。

下面是静态文件的示例设置。

STATIC_URL = '/static/'

# 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.
    os.path.join(BASE_DIR, 'static'),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # django.contrib.staticfiles.finders.DefaultStorageFinder',
)

并在您的模板(html 文件)中,这样称呼它:

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static "css/bootstrap.min.css" %}" />

您需要在放置 CSS 和 JS 文件的地方声明一个静态文件夹。

参考https://docs.djangoproject.com/en/1.7/howto/static-files/如何设置静态文件夹

示例实现https://github.com/Dsupreme/fests-django(关注 css 文件的放置位置以及它们的调用方式)