如何在django中添加模板css js和bootstrap

时间:2018-04-12 11:03:09

标签: python html css django

我想在我的django应用中添加bootstrap模板。我已将其下载并保存在我的静态文件夹中,然后将其路径添加到setting.py文件中:

STATIC_URL = '/static/'

模板的index.html是:

{% load staticfiles %}

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">

<title>Business Casual - Start Bootstrap Theme</title>

<!-- Bootstrap Core CSS -->

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

<!-- Custom CSS -->
<link href="{% static 'css/business-casual.css' %}" rel="stylesheet">

<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800" rel="stylesheet" type="text/css">
<link href="https://fonts.googleapis.com/css?family=Josefin+Slab:100,300,400,600,700,100italic,300italic,400italic,600italic,700italic" rel="stylesheet" type="text/css">

<!-- HTML5 Shim and Respond.js 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/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<div class="brand">Business Casual</div>
<div class="address-bar">3481 Melrose Place | Beverly Hills, CA 90210 

...

但不起作用,请帮帮我

1 个答案:

答案 0 :(得分:2)

如果您从python manage.py runserver开始运行此操作,则需要在urls.py

中加入以下内容
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

如果您在生产中运行它,请确保您已运行python manage.py collectstatic

您还需要settings.py中的以下内容,将项目替换为您的应用

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "project/static"),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')