Django-bootstrap3插件搭建Django+Bootstrap网站

 Django有个bootstrap3插件,可以简化对bootstrap3的使用,github上有其的项目:https://github.com/dyve/django-bootstrap3

打开帮助可以看其具体使用方法:

  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>

4.更多细节参考官方的标签说明:http://django-bootstrap3.readthedocs.io/en/latest/templatetags.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>

 

  • 2
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值