django template extends not working

本小妞迷上赌 提交于 2019-11-30 23:13:21

问题


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.


回答1:


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




回答2:


For more people who end up here (as myself), main thing to note is that when you use {% extends 'something.html' %}, you cannot use anything other than these template tags at the top-level.

You can obviously have html tags inside these tags (like block tags), but don't put ANYTHING outside the template tags.



来源:https://stackoverflow.com/questions/8688569/django-template-extends-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!