1

I know i can have common stuff like this in django

<html>

{% include "header.html" %}

<div id = 'content'>
blah
</div>

{% include "footer.html" %}
</html>

but it seems i need to pass data footer.html and header.html every time i include these pages?

Thanks

user79762
  • 45
  • 4

4 Answers4

3

Django's template inheritance may be just what you want in terms of not repeating the includes and other boilerplate. If by "pass data" you mean in the rendering context, e.g. there is some data you always want to put there, simplest is to make all your rendering contexts with a factory function that fills in the common parts of the data.

Edit: for the factory function I have in mind nothing especially fancy, just e.g. a simple class:

class ContextFactory(object):
  def __init__(self, **pervasive):
    self.pervasive = pervasive
  def makeContext(self, current):
    return dict(self.pervasive, **current)
  def registerPervasive(self, name, value):
    self.pervasive[name] = value

(etc, if you need more functionality there) then instantiate a single instance contextFactory in some appropriate module of yours. Where your views might currently be rendering with context dicts such as {'foo': bar}, you will instead use contextFactory.makeContext({'foo': bar}) to give the context factory a chance to inject whatever name/value pairs are currently registered with it -- that's all, really.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • thanks a lot, is there detailed documentation about "make all your rendering contexts with a factory function"? – user79762 Jul 19 '09 at 04:42
  • no, I haven't seen documentation (detailed or otherwise) about this specific application of them, but "factory" design patterns are well known since the Gang Of Four, and this is quite a simple case: instead of just passing a literal dict as the context, call your own makeContext function (with the dict as the argument) and it will enrich the dict, if and as needed, with whatever "pervasive data objects" you have registered with it (which it keeps as another dict) -- it basically just merges the two dicts by `return dict(self.pervasive,**current)`! – Alex Martelli Jul 19 '09 at 06:52
0

You don't need to pass data each time if you write your own tag https://docs.djangoproject.com/en/1.4/howto/custom-template-tags/

  1. Write your custom template tag
  2. Your tag implementation is able to retrieve data from database, from HTTP request header etc.
  3. Use your tag in your base.html template

Example: Let's say you want to have menu in each site. The create a menu tag and use it like this:

{% load my_tags %} <!-- don't forget to load your tags first -->
<html>

    {% menu %} <!-- your menu will be rendered here -->

    <div id = 'content'>
    blah
    </div>
</html>

Your tag can even put new variable in template context: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#setting-a-variable-in-the-context

Michal Vician
  • 2,486
  • 2
  • 28
  • 47
0

This also interests me as a newcomer to Django with ASP.NET background. I'm interested in making 'standalone' components that can be included in some views, but I don't want to add data for them in every view method if they're not going to be needed in the view. If I need to construct a context object that knows about data needed by all possible includes then things get more coupled.

I noticed somewhere else that this might be possible to do with a custom tag that sets data for the included template. It seems a cleaner solution to me even if it needs some more programming. An example of usage can be found at http://docs.djangoproject.com/en/dev/ref/contrib/comments/#ref-contrib-comments-index

rslite
  • 81,705
  • 4
  • 44
  • 47
0

See Why on earth do I have to pass RequestContext in all of my responses?

Apparently this is such a common pain that there's a package called django-annoying to fix that (among other things).

Community
  • 1
  • 1
tpk
  • 2,041
  • 5
  • 21
  • 29