Django有个bootstrap3插件,可以简化对bootstrap3的使用,github上有其的项目。
打开帮助可以看其具体使用方法:
- 第一步就是安装插件 pip install django-bootstrap3
- 第二步,在settings.py上的INSTALLED_APPS中加入 'bootstrap3,'
- 在模版中,加载bootstrap3库,用 bootstrap_*的标签,如下: 更多细节参考官方的标签说明
{# Load the tag library #}
{% load bootstrap3 %}
{# Load CSS and JavaScript #}
{% bootstrap_css %}
{% bootstrap_javascript %}
{# Display django.contrib.messages as Bootstrap alerts #}
{% bootstrap_messages %}
{# Display a form #}
<form action="/url/to/submit/" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Submit
</button>
{% endbuttons %}
</form>
{# Read the documentation for more information #}
实例,用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">
{% 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>
重新运行,结果正常: