设为首页 加入收藏

TOP

三、模板变量及模板过滤器(二)
2019-05-23 16:12:32 】 浏览:110
Tags:模板 变量 过滤器
ast }}</h2> 20 <h2>这是列表的一个值:{{ lt.0|add:5 }}</h2> 21 <h2>这是一个函数:{{ func|capfirst }}</h2> 22 <h2>这是一个类对象:{{ car }}</h2> 23 <h2>这是一个类对象属性:{{ car.brand }}</h2> 24 <h2>这是一个类对象方法:{{ travel|default:"nothing"|add:' haha'|title }}</h2> 25 <p>用户名:<input type="text"></p> 26 <p>密码:<input type="password"></p> 27 <p><input type="submit" value="登录"></p> 28 </form> 29 </body> 30 </html>

最后效果

4 自动转义

自动转义是将变量的一些特殊字符,比如左箭头(<)、右箭头(>)转义成html代码,这样做的目的是为了处理一些不安全的变量。

转义前 转义后
< &lt;
> &gt;
&#39;
&quot;
& &amp;
 

5 静态文件的引用

  • 在项目目录下创建static的目录, 为了区分开各种类型的文件分别创建css,image,js的目录.
  • 在settings.py文件中添加STATICFILES_DIRS,设置静态文件目录路径,同templates。
1 STATIC_URL = '/static/'
2 STATICFILES_DIRS = [
3     os.path.join(BASE_DIR, 'static')
4 ]
  • 引用
    • 创建模板/templates/studentlogin.html
 1 {% load static %}
 2 <!DOCTYPE html>
 3 <html lang="zh-CN">
 4   <head>
 5     <meta charset="utf-8">
 6     <meta http-equiv="X-UA-Compatible" content="IE=edge">
 7     <meta name="viewport" content="width=device-width, initial-scale=1">
 8     <meta name="description" content="">
 9     <meta name="author" content="">
10     <title>Signin Template for Bootstrap</title>
11     <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
12     <link href="{% static 'student/css/signin.css' %}" rel="stylesheet">
13   </head>
14 </html>
15 <!-- 仅有部分代码 -->
    • 创建/static/student/css/signin.css
 1 body {
 2   padding-top: 40px;
 3   padding-bottom: 40px;
 4   background-color: #eee;
 5 }
 6 
 7 .form-signin {
 8   max-width: 330px;
 9   padding: 15px;
10   margin: 0 auto;
11 }
12 .form-signin .form-signin-heading,
13 .form-signin .checkbox {
14   margin-bottom: 10px;
15 }
16 .form-signin .checkbox {
17   font-weight: normal;
18 }
19 .form-signin .form-control {
20   position: relative;
21   height: auto;
22   -webkit-box-sizing: border-box;
23      -moz-box-sizing: border-box;
24           box-sizing: border-box;
25   padding: 10px;
26   font-size: 16px;
27 }
28 .form-signin .form-control:focus {
29   z-index: 2;
30 }
31 .form-signin input[type="email"] {
32   margin-bottom: -1px;
33   border-bottom-right-radius: 0;
34   border-bottom-left-radius: 0;
35 }
36 .form-signin input[type="password"] {
37   margin-bottom: 10px;
38   border-top-left-radius: 0;
39   border-top-right-radius: 0;
40 }
    • student/views.py中引入
1 def login(request):
2     return render(request, 'student/login.html')
    • student/urls.py中引入
1 urlpatterns = [
2     path('login/', views.login),
3 ]
  • 效果图

 

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python列表推导式和嵌套的列表推.. 下一篇刨根问底:if 后怎么就可以跟对象..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目