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

yipeiwu_com5年前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利用ElementTree模块处理XML的方法详解

前言 最近因为工作的需要,在使用 Python 来发送 SOAP 请求以测试 Web Service 的性能,由于 SOAP 是基于 XML 的,故免不了需要使用 python 来处理...

python读取tif图片时保留其16bit的编码格式实例

tif图片的编码格式一般是16bit的,在使用python-opencv读取tif文件时,为了保留其编码格式,我们需要用以下的方式: import numpy as np impor...

python 正则表达式 概述及常用字符

1.元字符: . 它匹配除了换行字符外的任何字符,在 alternate 模式(re.DOTALL)下它甚至可以匹配换行 ^ 匹配行首。除非设置 MULTILINE 标志,它只是匹配字符...

如何用Python来理一理红楼梦里的那些关系

如何用Python来理一理红楼梦里的那些关系

前言 今天,一起用 Python 来理一理红楼梦里的那些关系 不要问我为啥是红楼梦,而不是水浒三国或西游,因为我也鉴定的认为,红楼才是无可争议的中国古典小说只巅峰,且不接受反驳!而红楼...

使用Python脚本在Linux下实现部分Bash Shell的教程

对于Linux用户来说,命令行的名声相当的高。不像其他操作系统,命令行是一个可怕的命题,但是对于Linux社区中那些经验丰富的大牛,命令行却是最值得推荐鼓励使用的。通常,命令行对比图形用...