Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't add extra_context to a ListView

Tags:

django

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?

like image 357
Anthony Lozano Avatar asked Dec 16 '22 04:12

Anthony Lozano


1 Answers

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
like image 87
Chris Pratt Avatar answered Jan 14 '23 01:01

Chris Pratt