Reputation: 621
After following the tutorial for Django, I have the baseline of my app up and running, but now I am trying to add some data to one of the templates. I thought I could add this by using extra_context but I am missing something (likely obvious, as I am new to Django). This is what I have in my app's urls.py:
url(r'^$', ListView.as_view(
queryset=Solicitation.objects.order_by('-modified'),
context_object_name='solicitationList',
template_name='ptracker/index.html',
extra_context=Solicitation.objects.aggregate(Sum('solicitationValue'),Sum('anticipatedValue')),
name='index',
)),
The error I am getting is TypeError :
ListView() received an invalid keyword 'extra_context'
What I need to do is somehow get those sums out to the template so that I can display them. How can I do this properly or easily?
Upvotes: 5
Views: 10601
Reputation: 239460
extra_context
requires a dict, i.e.:
extra_context={'solicitations': Solicitation.objects...}
EDIT
Sorry, actually, I don't think as_view
actually supports that kwarg. You can try it, but most likely you'll need to subclass the view and override get_context_data
as the docs describe:
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(PublisherBookListView, self).get_context_data(**kwargs)
# Add in the publisher
context['publisher'] = self.publisher
return context
Upvotes: 11
Reputation: 1042
Django, 1.5 seems do not support extra_context in as_view. But you can define a subclass and override get_context_data. I think this is better:
class SubListView(ListView):
extra_context = {}
def get_context_data(self, **kwargs):
context = super(self.__class__, self).get_context_data(**kwargs)
for key, value in self.extra_context.items():
if callable(value):
context[key] = value()
else:
context[key] = value
return context
Still, the extra_context should be a dict.
Hope this helps :)
Upvotes: 1