extends isn't working

729 views
Skip to first unread message

DjangoFett

unread,
Oct 22, 2007, 10:57:57 AM10/22/07
to Django users
I'm very new to the whole Django framework, but have a bit of
experiance in Python. I'm doing some of the tutorial work and
extending to my own project slowly. However, I'm having a MAJOR
problem when using the TEMPLATE_DIRS. In the settings file I have set
where all my template HTML files are to be. I, so far, have two files:
index.html and index_title.html (which "extends index.html").
index.html works without a hitch. However, I can't get any of the
extended files to work with index. Here's a snippit:


index.html:
...
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>{% block tag_title %}THIS IS TEMPORARY{% endblock %}</title>
</head>
...

index_title.html:

{% extends "index.html" %}

{% block tag_title %}THIS IS NOT TEMPORARY{% endblock %}

I've tried various forms of this, but to no avail. I can zip up the
project and forward it along to anyone who would be willing to help.
It doesn't seem like it would be a hard problem for someone who knows
Django well.

randal...@gmail.com

Karen Tracey

unread,
Oct 22, 2007, 11:19:06 AM10/22/07
to django...@googlegroups.com
What are the outwardly-visible signs of "I can't get any of the extended files to work with index"?  From what you've posted I don't know if you are seeing errors about templates files not being found or if you are just seeing unexpected output in your resulting HTML.  You might also want to mention what template loaders you have specified, and give a snippet of you view code that is attempting to render the problem tempalte.

Karen

DjangoFett

unread,
Oct 22, 2007, 11:45:35 AM10/22/07
to Django users
Sorry for the vagueness in my post.

The problem is that there are no errors. The output is just
unexpected. The text and code I put into the file which is index's
extension doesn't show up. I put in a default value between the
index.html block. Then I've tried to extend the block with an
alternate text and actual code to draw info from the content system I
have. In neither case does the alternate text or code show up. Here is
the code in the views.py that renders the page I'm having problems
with:

from django.http import HttpResponse
from django.template import Context, loader

def Index( request ):

t = loader.get_template( 'index.html' )
c = Context( { 'title': "EasyAutoSales.com Best Car Site Ever", } )

I've tried to use "t = loader.select_template( [ 'index.html',
'index_title.html', 'index_body.html' ] )" as well without any luck.
Thanks for the quick reply!

Randall Prince

DjangoFett

unread,
Oct 22, 2007, 11:50:08 AM10/22/07
to Django users
I misesd the return line...

return HttpResponse( t.render( c ) )

Karen Tracey

unread,
Oct 22, 2007, 11:54:05 AM10/22/07
to django...@googlegroups.com
On 10/22/07, DjangoFett <randal...@gmail.com> wrote:
def Index( request ):

  t = loader.get_template( 'index.html' )

I think you want:

t = loader.get_template( 'index_title.html' )

there.  Your view code needs to specify the "extending" template you want to have rendered, not the base template being extended. 

Karen

DjangoFett

unread,
Oct 22, 2007, 12:10:07 PM10/22/07
to Django users
How, then, can i extend multiple code sources off of my index.html
file?

I'm looking at the Django Template language page (http://
www.djangoproject.com/documentation/templates/) and what it's saying
(as well as my logic) isn't meshing with what you're saying. Here's a
simple example I'm creating right now for educational purposes:

index_title.html:

{% extends "index.html" %}
{% block tag_title %}THIS IS THE TITLE{% endblock %}

index.html:

<html>
<head>
<title>{% block block_title %}THIS IS NOT THE TITLE{% endblock %}</
title>
</head>
<body>
{% block block_header %}THIS IS NOT THE HEADER{% endblock %}<br />
<br />
{% block block_body %}THIS IS NOT THE BODY{% endblock %}<br />
<br />
{% block block_footer %}THIS IS NOT THE FOOTER{% endblock %}<br />
<br />
</body>
</html>

I'm going to load the "index_title.html" file? How can I load the
proper files to extend the header, body and footer section then? It
just seems like reverse logic to me.

On Oct 22, 11:54 am, "Karen Tracey" <kmtra...@gmail.com> wrote:

DjangoFett

unread,
Oct 22, 2007, 12:37:16 PM10/22/07
to Django users
I think I'm starting to see the point of extends now, as I had been
assuming it was equivilant to the php include statements. What I'm
assuming extends is for now is to always have "base" files from which
to extend. You create particular layouts from which children inherit
the parents overall template.

So, example:

You'll write up index_viewlist.html which only has the individual
special "blocks" of code which are then used in the index.html
template (index_viewlist.html->index.html). Then, you'll have another
file called index_viewitem.html which will use the same block names to
form new code using the index.html template (index_viewitem.html-
>index.html).

I was hoping for more of a tree structure of extensions...but I see
how that would breakdown when you want to use various views to inheret
from a single index file using their own methods to put data into that
index. Long story short, it's very much akin to templated classes.

Randall Prince

On Oct 22, 12:10 pm, DjangoFett <randallpri...@gmail.com> wrote:
> How, then, can i extend multiple code sources off of my index.html
> file?
>

> I'm looking at the Django Template language page (http://www.djangoproject.com/documentation/templates/) and what it's saying

Karen Tracey

unread,
Oct 22, 2007, 12:54:59 PM10/22/07
to django...@googlegroups.com
On 10/22/07, DjangoFett <randal...@gmail.com> wrote:
I was hoping for more of a tree structure of extensions...

Note that extends can be chained.  Using your example:

You start with index.html that defines all the blocks you want to customize, as you have it now.

Then, in index_title, you extend index and customize the tag_title block.

Then, perhaps in index_body, you extend index_title (which has already customized tag_title), and customize block_header and block_body.

Then, perhaps in index_footer, you extend index_body and customize block_footer. 

At that point all your blocks have been customized and your view specifies index_footer as the template to load, which in turn points to index_body, which in turn refers to index_title, which finally gets back to the base index template.

Now this particular sequence of extensions for customization may not make much logical sense, but the point is you do not have to override/customize everything in the base template at once.  You can chain your extensions.  And by creating different extenders for the same base, create a tree of extensions.  Not sure if that's the kind of tree you were looking for, though.

Hoping this clarifies more than it muddies,
Karen

DjangoFett

unread,
Oct 22, 2007, 1:06:27 PM10/22/07
to Django users
The more I "fiddle" with these templates, the more I'm understanding
them. It was the initial learning curve that just had me confused b/c
it wasn't what I was looking for. I think I understand all you're
speaking of now, though. Thanks for your great help Karen! I'll be
seeing you on here a lot with my new found love of Django!

Randall

On Oct 22, 12:54 pm, "Karen Tracey" <kmtra...@gmail.com> wrote:

Ramiro Morales

unread,
Oct 22, 2007, 3:20:39 PM10/22/07
to django...@googlegroups.com
Randall,

On 10/22/07, DjangoFett <randal...@gmail.com> wrote:
>

> The more I "fiddle" with these templates, the more I'm understanding
> them. It was the initial learning curve that just had me confused b/c

> it wasn't what I was looking for...

There is a description of how template inheritance works in the no-tutorial
documentation:

http://www.djangoproject.com/documentation/templates/#template-inheritance

--
Ramiro Morales

Reply all
Reply to author
Forward
0 new messages