tags:

views:

22

answers:

1

Hi, everyone. I'm just starting with django, so sorry for a possibly stupid question.

Imagine, I have a webpage with 3 logical pieces: content, dynamic menu, banners. It seems ok to me to have a template with something like:

{% include "banners.html" %}
{% include "menu.html" %}
{% include "content.html" %}

But then, how do I do it? Every piece has its separate logic in a separate view in its' separate app. How do I trigger execution of all three views and render all three templates?

P.S. Is there a sourceforge-like site for django apps where I could take a look at how people are doing stuff in real projects?

+2  A: 

The standard way to do it is something as follows:

Templates:

  • Have a base.html that has the banners, menu and a body block which is empty.
  • For each template, extend the base.html and override the body block.

    {% extends "base.html" %}
        {% block body %}
        -- Your this page's content goes here.
        {% endblock %}
    
  • You can use the includes, where necessary, but prefer the extends where possible. Include was debated to be included in the template language.

Populating Context:

You now have a lot of templates with placeholders that need to be replaced with the real "context" values.

  • Pass the RequestContext, that should contain many standard requirements for the template.
  • For the values that you need in every template, write a template context processor.
  • Those context's that you need in this template, you populate in the view.

Using multiple app's views:

  • Most apps written for reuse will include template_name as a standard parameter, along with extra_context. You will need to call those views with these parameters.
  • Some apps go out of their way, to create a lazily evaluated response (like TemplateResponse) so that you can grab the context they populate, in your view.

Django reusable apps:

Are you kidding me? They are all over the Internet!

http://github.com/search?q=django&type=Everything

http://bitbucket.org/repo/all/?name=django

http://code.google.com/hosting/search?q=django&projectsearch=Search+projects

Lakshman Prasad
+1 for "Are you kidding me?" ;-)
celopes