[英]Bootstrap static files not being included in Django template
我是Django的新手,在Django頁面上呈現引導程序時遇到了問題。
這是我的基本html,
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Dropbox Web App Prototype</title>
<!-- Bootstrap -->
<link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="jumbotron">
<h1>Hello, world!</h1>
<p>This is a template showcasing the optional theme stylesheet included in Bootstrap. Use it as a starting point to create something more unique by building on or modifying it.</p>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="{% static 'bootstrap/js/bootstrap.min.js' %}"></script>
</body>
</html>
我還在我的settings.py文件中添加了以下行
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
CSS不在主頁上呈現。 任何幫助表示贊賞。
BASE_DIR
默認定義為:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
__file__
實際上是settings.py
,因此BASE_DIR
在父目錄中,該目錄包含manage.py
。
看來您的static
文件夾在另一個級別上,因此只需將其移至與manage.py
相同的級別即可,它應該可以工作。
只需在您的設置中定義附加靜態網址,如下所示:
STATIC_URL ='/靜態/'
並再次檢查是否提供了引導程序
CDN:
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
靜態的
在django runserver的網址中:
if settings.DEBUG:
urlpatterns += [url(r'^static/(?P<path>.*)$', views.serve),]
從Web服務器運行時,您需要配置別名。 Nginx示例:
location /static {
alias /home/username/mysite/static;}
settings.py:
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, "static"),)
您可以粘貼BASE_DIR嗎? 您可以將其設置為: BASE_DIR to BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
在html中替換此
<link href="{% static 'bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">
有了這個
<link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
並檢查是否在設置中。
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
您使用的舊語法可能與您的Django版本不匹配。 在您的html文件中進行更改:
{% load staticfiles %}
至
{% load static %}
另外,將單引號替換為雙引號:
"{% static 'bootstrap/css/bootstrap.min.css' %}"
至:
"{% static "bootstrap/css/bootstrap.min.css" %}"
您可以在此處查看處理靜態文件的確切語法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.