经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Django » 查看文章
详解Django项目中模板标签及模板的继承与引用(网站中快速布置广告)
来源:jb51  时间:2019/3/27 9:56:04  对本文有异议

Django项目中模板标签及模板的继承与引用

常见模板标签

  1. {% static %}
  2. {% for x in range(x) %}{% endfor %}
  3. 循环的序号{% forloop %}
  4. 循环的序号反向排列,从1开始计算,从0开始计算在后面加上0{% forloop.revcounter0 %}
  5. {% if condition1 %}sentence1{% else condition2 %}sentence2{% endif %}

模板标签url反向解析

视图函数

  1. def student_detail_view(request,pk):
  2. students = {
  3. 1:{'id':1,'name': '小明', 'age': 18, 'sex': '男'},
  4. 3:{'id':3,'name': '小花', 'age': 17, 'sex': '女'},
  5. 19:{'id':19,'name': '小李', 'age': 18, 'sex': '男'},
  6. 100:{'id':100,'name': '小红', 'age': 18, 'sex': '女'},
  7. }
  8. if pk in students:
  9. student = students[pk]
  10. else:
  11. student = '查无此人'
  12. return render(request,'teacher/student_detail.html',context={'student':student})

url反向解析应用模板

  1. <tbody>
  2. {% for student in students %}
  3. <tr {% if student.sex == '女' %}style="background-color: pink"{% else %}style="background-color: aqua"{% endif %}>
  4. <td><a href="{% url 'teacher:student_detail' student.id %}">{{ student.id }}</a></td>
  5. <td>{{ student.name }}</td>
  6. <td>{{ student.age }}</td>
  7. <td>{{ student.sex }}</td>
  8. </tr>
  9. {% endfor %}
  10. </tbody>

学生详情页:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. 学生详情页:
  9. {{ student }}
  10. </body>
  11. </html>

模板的继承与引用

为什么要有模板的继承与引用?

学前端的时候写的页面比较复杂,每个页面都有相同的地方。

模板的继承

首先,新建一个父类页面。 挖好坑1和坑2。

  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. <!-- 上述3meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  9. <!-- 挖坑1,通过模板标签block来实现模板的继承与引用 -->
  10. <title>{% block title %}{% endblock %}</title>
  11.  
  12. <!-- Bootstrap -->
  13. <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
  14.  
  15. <!-- HTML5 shim Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
  16. <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
  17. <!--[if lt IE 9]>
  18. <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  19. <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
  20. <![endif]-->
  21. </head>
  22. <body>
  23. <!-- 挖坑2,通过模板标签block来实现模板的继承与引用 -->
  24. {% block content %}{% endblock %}
  25.  
  26. <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
  27. <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
  28. <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
  29. <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
  30. </body>
  31. </html>

其次,子类页面的继承。

  1. {% extends 'teacher/base.html' %}
  2. {% block title %}学生列表{% endblock %}
  3. {% block content %}
  4. <h1>学生列表</h1>
  5. <table class="table">
  6. <thead>
  7. <tr>
  8. <th>ID</th>
  9. <th>姓名</th>
  10. <th>年龄</th>
  11. <th>性别</th>
  12. </tr>
  13. </thead>
  14. <tbody>
  15. {% for student in students %}
  16. <tr {% if student.sex == '女' %}style="background-color: pink"{% else %}style="background-color: aqua"{% endif %}>
  17. <td><a href="{% url 'teacher:student_detail' student.id %}">{{ student.id }}</a></td>
  18. <td>{{ student.name }}</td>
  19. <td>{{ student.age }}</td>
  20. <td>{{ student.sex }}</td>
  21. </tr>
  22. {% endfor %}
  23. </tbody>
  24. </table>
  25. {% endblock %}

最终效果展示:

Attention:

  • 一般情况一层继承就够了,多层继承不好,因为容易出错
  • 模板的继承要先在父类页面挖坑,子类页面可以填坑

模板的引用

首先,创建一个被引用的广告页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. a{
  8. text-decoration: none;
  9. position: fixed;
  10. bottom: 0;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1><a href="https://www.baidu.com/" id="ad">这是一个广告!不要点不要点</a></h1>
  16. <script>
  17. var h = document.getElementById('ad');
  18. var color = 'blue';
  19. function change_color() {
  20. if(color == 'blue'){
  21. color = 'red';
  22. }else{
  23. color = 'blue';
  24. }
  25. h.style.color = color;
  26. setTimeout('change_color()',500)
  27. }
  28. change_color()
  29. </script>
  30. </body>
  31. </html>

其次,在页面中引用被引用的页面。

这里我们是在一个父类页面中引用的被引用页面

关键代码是下面的引用语句

  1. {% include 'teacher/ad.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. <!-- 上述3meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
  9. <title>{% block title %}Bootstrap{% endblock %}</title>
  10. <!-- 引用广告页面! -->
  11. {% include 'teacher/ad.html' %}
  12. <!-- Bootstrap -->
  13. <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
  14.  
  15. <!-- HTML5 shim Respond.js 是为了让 IE8 支持 HTML5 元素和媒体查询(media queries)功能 -->
  16. <!-- 警告:通过 file:// 协议(就是直接将 html 页面拖拽到浏览器中)访问页面时 Respond.js 不起作用 -->
  17. <!--[if lt IE 9]>
  18. <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  19. <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
  20. <![endif]-->
  21. </head>
  22. <body>
  23. {% block content %}
  24.  
  25. {% endblock %}
  26.  
  27. <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery,所以必须放在前边) -->
  28. <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
  29. <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
  30. <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
  31. </body>
  32. </html>

如果将引用语句加在父类页面,那么继承父类页面的子页面都会有被引用的页面效果

效果展示如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号