On Thu, 2006-06-22 at 11:52 +0100, Frankie Robertson wrote:
> On 22/06/06, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
> >
> > On Thu, 2006-06-22 at 10:01 +0000, Jaroslaw Zabiello wrote:
> > > Django uses Python modules instead of classes. The problem is how to
> > > avoid duplication of the code in controllers when I want to put some
> > > data to parent templates? Let see the example:
> > >
> > > base.html:
> > > {% block welcome %}
> > > Hello {{ name }}!
> > > {% endblock %}
> > > {% block main %}{% endblock %}
> > >
> > > test1.html:
> > > {% extends "base.html" %}
> > > {% block main %}blah, blah{% endblock %}
> > >
> > > test2.html:
> > > {% extends "base.html" %}
> > > {% block main %}different blah, blah{% endblock %}
> > >
> > > How to deal with the same, shared blocks? Do I have to copy code for
> > > all shared blocks? It looks very bad.
> > >
> > > views.py:
> > > def test(request):
> > >     return render_to_response('test.html', {'name':'Jarek'})
> > > def test2(request):
> > >     return render_to_response('test2.html', {'name':'Jarek'})
> > >
> > > I would like to have something like RoR, where all shared variables for
> > > all shared (partials) templates can be set in one, and only one, place:
> >
> > If only some of you views need this default data, make a single function
> > that populates the default or standard data and returns a dictionary.
> > Then your specific view functions can call that function initially and
> > subsequently update the dictionary with their changes. That's pretty
> > standard programming practice -- factor out the common bit (just like in
> > your Ruby example).
> >
> > If every single view needs the same piece of data added to the context,
> > write a template context processor (see [1]), such as how the auth
> > middleware adds the "user" variable into the context.
> >
> 
> In both of these cases, how do you get the same thing to happen with
> generic views.

Each generic view takes both an extra_context dictionary, which can
contain callables, and a list of context_processors, which can do what
you want. These are both documented for every view in the generic views
documentation. Or you can use custom template tags as Matt McDonald
suggests in another post in this thread.

Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to