Reputation: 795
I have a model called Account
, with has a model function (see code below). I would like to show in my template the users total on every page. The only way I can think of doing this in putting accounts in every view. What other options do I have? Can it be liked to request.user? I already have access to request in every view and Accounts has the FK user.
def _balance(self):
aggregates = self.transactions.aggregate(sum=Sum('amount'))
sum = aggregates['sum']
return D('0') if sum is None else sum
Upvotes: 0
Views: 52
Reputation: 8354
You want a context processor. Every time you render a template, you give it a “context”; this is a dictionary-like object whose keys are variable names and whose values are the values of the variables.
For example..
Create a file my app/context_processors
def get_balance(request):
do something
return {'balance': 0}
add it to your settings:
TEMPLATE_CONTEXT_PROCESSORS = ('myapp.context_processors.get_balance',)
done.
Upvotes: 4