django使用html模板减少代码代码解析

yipeiwu_com6年前Python基础

看下面两个页面:

一个显示文章列表,一个显示文章详细信息,其中的部分内容相同,有可以重用的部分。

所有就此例可以设置三个html文件:重用部分,目录部分,文章部分。

重用部分:

base.html

<!DOCTYPE html>
<!DOCTYPE html>
<html lang="zh-CN">
{% load staticfiles %}
 <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>首页</title>

  <script type="text/javascript" src="{% static "bootstrap/js/jquery-2.0.0.min.js" %}"> </script>
  <script type="text/javascript" src="{% static "bootstrap/js/jquery-ui.js" %}"></script>
  <link href="{% static " rel="external nofollow" bootstrap/css/bootstrap-combined.min.css" %}" rel="stylesheet" media="screen" >
  <script type="text/javascript" src="{% static "bootstrap/js/bootstrap.min.js" %}"s></script>
</head>

<body>
<div class="container-fluid" id="LG">
  <div class="row-fluid">
    <img src="{% static "img/head1.png" %}" alt="返回主页">
    <div class="span12" >
    </div>
  </div>

  <div class="row-fluid">
    <div class="span2">
    </div>
    <div class="span6">
      <ul class="nav nav-tabs">
        <li class="active">
          <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a>
        </li>
        <li>
          <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >资料</a>
        </li>
        <li >
          <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" >信息</a>
        </li>
      </ul>
      {% block context %}
      添加内容
      {% endblock context %}
    </div>
    <div class="span4">
    </div>
  </div>
</div>
</body>
</html>

使用{%blockcontext%}{%endblockcontext%}标签,添加不同内容

目录部分

index.html

{% extends "blog/base.html" %}
  {% block context %}
      {% if latest_article_list %}
      <ul>
        {% for article in latest_article_list %}
        <li>
          <a href="{% url 'blog:detail' article.id %}" rel="external nofollow" >
           {{ article.title }} </a>
        </li>
           {% endfor %}
      </ul>
      {% else %}
        <p>No articles are available.</p>
      {% endif %}
  {% endblock context %}

使用{%extends"blog/base.html"%}载入模板文件,模板文件的位置为相对于templates的路径。

文章部分:

detail.html

{% extends "blog/base.html" %}
{% block context %}
<h1>{{ article.title }}</h1>
<p>{{ article.content }}</p>
{% endblock context %}

django文档地址:http://python.usyiyi.cn/django_182/ref/templates/language.html

总结

以上就是本文关于django使用html模板减少代码代码解析的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:

浅谈Django REST Framework限速

Django admin美化插件suit使用示例

Django admin实现图书管理系统菜鸟级教程完整实例

如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

Python排序算法实例代码

Python排序算法实例代码

排序算法,下面算法均是使用Python实现: 插入排序 原理:循环一次就移动一次元素到数组中正确的位置,通常使用在长度较小的数组的情况以及作为其它复杂排序算法的一部分,比如mergeso...

利用ctypes提高Python的执行速度

前言 ctypes是Python的外部函数库。它提供了C兼容的数据类型,并且允许调用动态链接库/共享库中的函数。它可以将这些库包装起来给Python使用。这个引入C语言的接口可以帮助我们...

解决使用PyCharm时无法启动控制台的问题

问题: 使用PyCharm时无法启动控制台? 今天打开PyCharm时突然无法启动控制台,IPython和Python本身都无法使用 解决: 很有可能你安装了较高版本的ipython...

Python打开文件、文件读写操作、with方式、文件常用函数实例分析

Python打开文件、文件读写操作、with方式、文件常用函数实例分析

本文实例讲述了Python打开文件、文件读写操作、with方式、文件常用函数。分享给大家供大家参考,具体如下: 打开文件: 在python3中,打开文件的函数是: open(file,...

python输入错误后删除的方法

python输入错误怎么删除? python常用的输入函数raw_input()在输入的过程中如果输错了,不能像在命令行下那样backspace取消已输入的字符,还得重新再输入。 怎么才...