【问题标题】:CSS in Django 2.0Django 2.0 中的 CSS
【发布时间】:2018-08-08 16:23:17
【问题描述】:

这是我的队友使用 CSS/Bootstrap 编写的 HTML 代码。 如何在 Django 中渲染它? 当我以 HTML 格式打开文件但无法在 Django 中呈现它时,它可以工作。

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">



<!-- Bootstrap Core CSS -->
<link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

<!-- Custom Fonts -->
<link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css">
<link href="vendor/simple-line-icons/css/simple-line-icons.css" rel="stylesheet">

<!-- Custom CSS -->
<link href="css/stylish-portfolio.min.css" rel="stylesheet">

【问题讨论】:

  • 你为什么不能?你试过什么?
  • 是的,我有这些错误 [08/Aug/2018 07:52:43] "GET /vendor/fontawesome-free/css/all.min.css HTTP/1.1" 404 2582未找到:/vendor/simple-line-icons/css/simple-line-icons.css 未找到:/css/stylish-portfolio.min.css [08/Aug/2018 07:52:43] “GET /vendor /bootstrap/css/bootstrap.min.css [08/Aug/2018 07:52:43] “GET /vendor/simple-line-icons/css/simple-line-icons.css HTTP/1.1”404 2615 [08 /Aug/2018 07:52:43]“GET /css/stylish-portfolio.min.css HTTP/1.1”404 2552 未找到:/js/stylish-portfolio.min.js 未找到:/js/contact_me.js未找到:/vendor/jquery/jquery.min.js
  • [08/Aug/2018 07:52:43] "GET /vendor/fontawesome-free/css/all.min.css
  • 这是怎么回事?看起来像 Apache 访问日志中的简单行
  • 你必须进入文档,或者阅读一些关于这个的教程。您没有正确链接字体和 css。该文档告诉您将它们存储在哪里以及如何在模板中链接它们。还有很多教程。祝你好运。

标签: css django bootstrap-4


【解决方案1】:

在您的设置中输入:

STATIC_URL = '/static/'
STATIC_ROOT = "path/to/your/static/folder"

在你的根 urls.py 中放在 urlpatterns 之后:

from django.conf import settings
from django.conf.urls.static import static

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

并在项目的根文件夹中创建静态文件夹,并将所有静态文件复制到那里。 之后在您的模板中:

{% load static %}

<link href="{% static 'vendor/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">

这样使用。 如果您还有其他问题,请阅读: https://docs.djangoproject.com/en/2.1/howto/static-files/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 2019-01-16
    • 2018-07-19
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多