views:

1488

answers:

2

I would like to provide the same content inside 2 different base files.

So I'm trying to do this:

page1.html:

{% extends "base1.html" %}
{% include "commondata.html" %}

page2.html:

{% extends "base2.html" %} 
{% include "commondata.html" %}

The problem is that I can't seem to use both extends and include. Is there some way to do that? And if not, how can I accomplish the above?

commondata.html overrides a block that is specified in both base1.html and base2.html

The purpose of this is to provide the same page in both pdf and html format, where the formatting is slightly different. The above question though simplifies what I'm trying to do so if I can get an answer to that it will solve my problem.

+4  A: 

When you use the extends template tag, you're saying that the current template extends another -- that it is a child template, dependent on a parent template. Django will look at your child template and use its content to populate the parent.

Everything that you want to use in a child template should be within blocks, which Django uses to populate the parent. If you want use an include statement in that child template, you have to put it within a block, for Django to make sense of it. Otherwise it just doesn't make sense and Django doesn't know what to do with it.

The Django documentation has a few really good examples of using blocks to replace blocks in the parent template.

http://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance

bigmattyh
my commondata.html has the block defined in it. But it is not replacing the parent tempalte's block... If instead of doing an include I write the exact data twice in both page1.html and page2.html then of course it does work. But I want to factor out that commonality into commondata.html.
Net Citizen
I'll try within a block but i think I tried that previously...
Net Citizen
Seems to work, I remember trying this but I must have had a typo or something at the time causing it not to work.
Net Citizen
Thank you for your help!
Net Citizen
np! glad to assist.
bigmattyh
see my answer below for why it didn't work for me the first time, I'll leave you with the accepted answer though because you answered the question I asked correctly.
Net Citizen
A: 

More info about why it wasn't working for me in case it helps future people:

The reason why it wasn't working is that {% include %} in django doesn't like special characters like fancy apostrophe. The template data I was trying to include was pasted from word. I had to manually remove all of these special characters and then it included successfully.

Net Citizen
oh, well that's a different problem entirely. :)
bigmattyh