search

Home  >  Q&A  >  body text

python - django模板继承问题求助

base.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">  
<html lang="en">  
<head>  
    <title>{% block title %}{% endblock %}</title>  
</head>  
<body>  
    <h1>My helpful timestamp site</h1>  
    {% block content %}{% endblock %}  
    {% block footer %}  
    <hr>  
    <p>Thanks for visiting my site.</p>  
    {% endblock %}  
</body>  
</html>  


{% extends "base.html" %}  
  
{% block title %}The current time{% endblock %}  
  
{% block content %}  
<p>It is now {{ current_date }}.</p>  
{% endblock %}  

很不理解 这个东西 怎么就继承了呢? base.html <title>{% block title %}{% endblock %}</title> 这么写是什么意思啊?

大家讲道理大家讲道理2893 days ago237

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 09:18:22

    You can understand it this way
    base.html is the master template
    a.html is the child template

    {% block title %}{% endblock %} You can understand it as a placeholder or variable
    As long as your a.html page inherits bash.html with {% extends "base.html" %}
    In a.html It contains all the content in base.html, and you can use {% block title %}abc{% endblock %} to modify the value of your subpage

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:18:22

    You can study the source code implementation process. As for <title>{% block title %}{% endblock %}</title> here is a simple Update mechanism

    reply
    0
  • Cancelreply