如何在django app中添加bootstrap模板

时间:2015-03-24 12:49:52

标签: django twitter-bootstrap templates

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

STATICFILES_DIRS = '/home/dc/my_django_apps/mutech_website/mutech/static/'

模板的index.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">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Freelancer - Start Bootstrap Theme</title>

    <!-- Bootstrap Core CSS - Uses Bootswatch Flatly Theme: http://bootswatch.com/flatly/ -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom CSS -->
    <link href="css/freelancer.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/css?family=Lato:400,700,400italic,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]-->

</head>

<body id="page-top" class="index">

    <!-- Navigation -->
    <nav class="navbar navbar-default navbar-fixed-top">
        <div class="container">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header page-scroll">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" href="#page-top">Start Bootstrap</a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li class="hidden">
                        <a href="#page-top"></a>
                    </li>
                    <li class="page-scroll">
                        <a href="#portfolio">Portfolio</a>
                    </li>
                    <li class="page-scroll">
                        <a href="#about">About</a>
                    </li>
                    <li class="page-scroll">
                        <a href="#contact">Contact</a>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container-fluid -->
    </nav>


    <!-- jQuery -->
    <script src="js/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="js/bootstrap.min.js"></script>

    <!-- Plugin JavaScript -->
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.3/jquery.easing.min.js"></script>
    <script src="js/classie.js"></script>
    <script src="js/cbpAnimatedHeader.js"></script>

    <!-- Contact Form JavaScript -->
    <script src="js/jqBootstrapValidation.js"></script>
    <script src="js/contact_me.js"></script>

    <!-- Custom Theme JavaScript -->
    <script src="js/freelancer.js"></script>

</body>

</html>

但仍然不起作用。任何建议将不胜感激。

2 个答案:

答案 0 :(得分:1)

首先configure the static files app,在setting.py中添加此设置

STATIC_URL = '/static/'

并确保所有静态文件/文件夹都位于根目录或静态文件夹中名为static的文件夹中,但确保运行collectstatic命令,然后从静态文件中引用JS文件请使用这个语法

<script src="{% static 'js/classie.js' %}"></script>

并用于链接CSS:

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

答案 1 :(得分:0)

您的settings.py

中需要3个变量
# Alias that will be written in the source code
STATIC_URL = '/public/'

# The actual folder where static files will be collected
STATIC_ROOT = 'static'

# Location of folders that will be added to static folder
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "folder_one", "folder_two", "static"),
)

在此之后,您需要运行命令

python manage.py colletstatic

将所有静态文件复制到STATIC_ROOT

在此之后,您可以使用模板中的静态文件。只需将标记添加到模板

即可
{% load staticfiles %}

并使用静态标记

{% static 'folder_one/folder_two/path_to_static' %}

您也可以使用

... src="/public/folder_one/folder_two/path_to_static" ...

但是如果你想在某一天更改别名,你就必须改变所有这些...所以,第一个选项要好得多

您必须检查静态文件夹以查看您必须使用的路径

django会写这样的东西

'/public/folder_one/folder_two/path_to_static'

当您完成这项工作时,请查看bowerbower-installer

第一个将使用单个命令下载所需的所有软件包。第二个将您需要的所有文件放在特定文件夹中。这非常好,因为您不必逐个下载。

bower.json示例

{
  "name": "your_project_name",
  "version": "1.0.0",
  "authors": [
     "you Name <your@email>"
  ],
  "license": "License type",
  "ignore": [
    "**/.*",
    "node_modules",
    "bower_components"
  ],
  "dependencies": {
     "jquery": "latest",
     "bootstrap": "latest"
  },
  "install": {
     "path": "where/do/you/want/your/files"
  }
}

只需添加更多依赖项即可完成。然后检查bower-installer。它只会将您需要的文件复制到目录中,而不是每个包的完整源代码。完成后,您可以删除bower文件夹,只留下bower-installer文件夹。