Luke
Luke

Reputation: 1814

Sidebar with django, layout or view?

I want to make a sidebar for my webapplication. It contains the following content:

I want to include this sidebar in every site/view. So my first guess is that it would belong to a layout. But it's also dynamic and as far as i know layouts are static.

How do I avoid redundancy in my views/layouts and still have the sidebar on every site?

Upvotes: 4

Views: 4513

Answers (2)

Rohan
Rohan

Reputation: 53386

For static content in your sidebar (e.g. search form), its straight forward template/html.

For the dynamic content like list of tags, recent posts: Once in the template you have identified a elements (div or something else) to put this info, you can populate its content using either your custom template tag or having custom context processor.

In your case, if the content doesn't really depend upon request parameter or url, template tag would be better choice.

Reference Custom template tag Custom Context Processor

Upvotes: 1

Bernhard Vallant
Bernhard Vallant

Reputation: 50806

To have context data passed to multiple templates you have different options in django; You could either:

  • Make a Template Tag which can pull in the relevant data and render it and reuse it in every template you need to (or just insert it in a base template and use template inheritance).

  • Use a context processor: It will be called with every request and add data to every view's context that uses RequestContext.

  • Using Django's class based views you could have all your views inherit from a base view which adds data to your context.

If you need data from your database I would rather go with using a template tag than using a context processor as it will be called for every view.

Upvotes: 3

Related Questions