django 模板扩展不起作用 [英] django template extends not working

查看:34
本文介绍了django 模板扩展不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 base.html

<头><标题>我的网站</title><身体><div id="包装器"><!-- 标题开始-->{% block nav %} {% endblock %}{% 块索引 %} {% endblock %}

这是我的 nav.html

{% 扩展 "base.html" %}{% 块导航 %}<div id="标题"><div class="inner"><div class="nav"><ul><li class="current"><a href="index.html">首页</a></li><li><a href="about.html">关于</a></li><li><a href="blog_right.html">博客</a></li><li><a href="contact.html">联系方式</a></li>

<div class="clear"></div></div><!-- .内端--></div><!-- #header end --><!-- 标题结束-->{% 结束块 %}

这是我的 index.html

{% 扩展 "base.html" %}{% 块索引 %}<p>你好</p>{% 结束块 %}

我以前做过几次,但我不知道为什么这不起作用?网址和视图位于此处.

解决方案

好吧,一切都很好,你遇到的麻烦是你很困惑,只是在 base 中命名一个块并不会调用它.标记扩展和包含之间的区别.你已经混淆了扩展到包含.

在您的视图中,如果您调用 say index.html,它将正确呈现.将view中的base.html改成index.html就可以达到你想要的效果.

希望这会有所帮助.更多内容可以在这里阅读:https://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance

This is my base.html

<!DOCTYPE html> 
<head> 
<title> My Site </title>
</head> 
<body>
<div id="wrapper">  
<!-- HEADER START -->
{% block nav %} {% endblock %}
{% block index %} {% endblock %}
</div> 
</body>
</html>

this is my nav.html

{% extends "base.html" %}
{% block nav %}
<div id="header"> 
<div class="inner">

<div class="nav"> 
<ul> 
<li class="current"><a href="index.html">Home</a></li> 
<li><a href="about.html">About</a></li> 
<li><a href="blog_right.html">Blog</a></li>                        
<li><a href="contact.html">Contact</a></li> 
</ul>                     
</div>  
<div class="clear"></div>           
</div><!-- .inner end --> 
</div><!-- #header end --> 
<!-- HEADER END -->
{% endblock %}

this is my index.html

{% extends "base.html" %}
{% block index %}
<p> hello </p>
{% endblock %}

I have done it several times before before but i am clueless as to why this is NOT working? the urls and views are here.

解决方案

Well everything is fine, the trouble that you are having is that you are confused, just naming a block in base does not calls it. Mark the difference between extends and include. You have counfused extends to include.

Once in your views if you call say index.html it will be rendered properly. The effect you want can be achieved by changing the base.html in your views to index.html.

Hope this helps. more can be read here: https://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance

这篇关于django 模板扩展不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆