Reputation: 8418
I am creating a simple web application and I need to have a right sidebar with the user's personal data. It is a dictionary of 20 strings and 10 model objects. I want this data set to be included in every web page. There are two options.
(a) I do something like this for every action:
data = {
'somedata', somedata #data created for this action needed for the page
}
data.update(ul.getRightSidebarContent(user)) #add all data to be shown for the user
variables = RequestContext(request, data)
return render_to_response("anypage.html", variables)
(b) create ajax actions and put them in the base.html template (which is inherited by all template pages) so at each page request ajax action will load the right side bar content.
Is there another solution for this? Which one is the best? Possible drawbacks for each solution? Can you elaborate, please?
Thanks, Chris
Upvotes: 1
Views: 329
Reputation: 667
I use custom template tag, it's very handy:
tags.py
from django import template
from my.models import model_name_or_*
register = template.Library()
@register.inclusion_tag('path/to/tag/template.html')
def my_tag():
return { 'myData': MyModel.objects.all(), }
@register.inclusion_tag('path/to/tag2/template.html')
def my_tag2():
return { 'myData2': MyModel.objects.all(), }
Tag template is what you want, example:
<ul>
{% for item in myData %}
<li>{{ item.name }}</li>
{% endfor %}
</ul>
And if you want use this tags in you template just type:
{% load tags %}
where tag is you tags file and use {% my_tag %}
Upvotes: 4
Reputation: 50786
From the django side there are two solutions for that:
You could use a context processor that adds this data to your context before rendering a page. I think it's a quite nice solution for your problem, but keep in mind that the context processors are only invoked if you are adding a RequestContext
object in all of your views-if you don't do that the processor's data will not be added. A drawback could be that context processors will be called with every of these views then, which could cause a little overhead if you don't need them; but I don't think this should be a problem for you...
You can write custom template tags that pull these data in the template, reuse them everywhere where you need them. It could be an advantage to have the template tags already render the data also in a template, so you don't have to deal in your templates with that anymore and simply put the template tag where you want the content to appear.
I think the first possibility to put it in every view's code is not releveant and you have to repeat a lot of things... Maybe the ajax solution makes sense if the generation of the outputted data may take long or uses another web service and you don't want to have a slowly loading page if you'd have to wait for that...
Upvotes: 5
Reputation: 80031
This seems like something you would fix with a context processor.
http://docs.djangoproject.com/en/dev/ref/templates/api/#writing-your-own-context-processors
Upvotes: 2