1. 程式人生 > >Django-bootstrap3外掛搭建Django+Bootstrap網站

Django-bootstrap3外掛搭建Django+Bootstrap網站

開啟幫助可以看其具體使用方法:

  1. 安裝外掛 pip install django-bootstrap3
  2. 在專案settings.py上的INSTALLED_APPS中加入 'bootstrap3,'
  3. 在模版中,載入bootstrap3庫,用 bootstrap_*的標籤,如下:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    {% load bootstrap3 %}
    {% bootstrap_css %}
    {% bootstrap_javascript %}
    {% bootstrap_messages %}
    <!-- Custom styles for this template -->
    <!--link href="../signin.css" rel="stylesheet"-->
    <link href="https://v3.bootcss.com/examples/signin/signin.css" rel="stylesheet">
  </head>
  <body>

    <div class="container">

      <form class="form-signin" action="/login_action/" method="post">
        <h2 class="form-signin-heading">使用者登入</h2>
        <label for="inputUsername" class="sr-only">username</label>
        <input type="text" id="inputUsername" name="username" class="form-control" placeholder="username" required autofocus>
        <label for="inputPassword" class="sr-only">password</label>
        <input type="password" id="inputPassword" name="password" class="form-control" placeholder="password" required>

        <button class="btn btn-lg btn-primary btn-block" type="submit" id="btn">登入</button>
         {{wronglyInput}}<br>
         {% csrf_token %}
      </form>

    </div> <!-- /container -->

  </body>
</html>

5.例項,用Django-bootstrap3改造 "Python web模版Django-22 Bootstrap美化HTML之用BootCDN"章中開發的index.html.

原始的html檔案:

<!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">
    <!-- 上述3個meta標籤*必須*放在最前面,任何其他內容都*必須*跟隨其後! -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">
 
    <title>Django Page</title>
 
    <!-- Bootstrap core CSS -->
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
 
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="https://v3.bootcss.com/assets/css/ie10-viewport-bug-workaround.css" rel="stylesheet">
 
    <!-- Custom styles for this template -->
    <link href="https://v3.bootcss.com/examples/signin/signin.css" rel="stylesheet">
 
    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="https://v3.bootcss.com/assets/js/ie-emulation-modes-warning.js"></script>
 
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
 
  <body>
 
    <div class="container">
 
      <form class="form-signin" action="/login_action/" method="post">
        <h2 class="form-signin-heading">釋出會管理</h2>
        <label for="inputUsername" class="sr-only">username</label>
        <input type="text" id="inputUsername" name="username" class="form-control" placeholder="username" required autofocus>
        <label for="inputPassword" class="sr-only">password</label>
        <input type="password" id="inputPassword" name="password" class="form-control" placeholder="password" required>
 
        <button class="btn btn-lg btn-primary btn-block" type="submit" id="btn">登入</button>
         {{wronglyInput}}<br>
         {% csrf_token %}
      </form>
 
    </div> <!-- /container -->
 
 
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <script src="https://v3.bootcss.com/assets/js/ie10-viewport-bug-workaround.js"></script>
  </body>
</html>

 改造:將上面綠色部分都刪掉,然後用Django-bootstrap3改造,把bootstrap中相關的css, javascript等標籤加入,如下

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="/static/js/jquery-1.12.4.min.js"></script>
    {% load bootstrap3 %}
    {% bootstrap_css %}
    {% bootstrap_javascript %}
    {% bootstrap_messages %}
    <!-- Custom styles for this template -->
    <!--link href="../signin.css" rel="stylesheet"-->
    <link href="https://v3.bootcss.com/examples/signin/signin.css" rel="stylesheet">
  </head>
  <body>

    <div class="container">

      <form class="form-signin" action="/login_action/" method="post">
        <h2 class="form-signin-heading">使用者登入</h2>
        <label for="inputUsername" class="sr-only">username</label>
        <input type="text" id="inputUsername" name="username" class="form-control" placeholder="username" required autofocus>
        <label for="inputPassword" class="sr-only">password</label>
        <input type="password" id="inputPassword" name="password" class="form-control" placeholder="password" required>

        <button class="btn btn-lg btn-primary btn-block" type="submit" id="btn">登入</button>
         {{wronglyInput}}<br>
         {% csrf_token %}
      </form>

    </div> <!-- /container -->

  </body>
</html>