Reputation: 1150
I am attempting to run more than two queries within my IndexView
, and display the results within my template. However, I can't seem to add both Entry.objects.filter(date=today_date)
and Savings.objects.filter(id=1)
to get_context_data
without it erroring out saying:
Reverse for 'entry-update' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['tracker/entry/update/(?P[0-9]+)/$']
How would I go about pulling data from two different models into one view? More specifically, how would I get something like this to work:
def get_context_data(self, **kwargs):
et = super(IndexView, self).get_context_data(**kwargs)
et['filter'] = Entry.objects.filter(date=today_date), (Savings.objects.filter(id=1))
return et
Thanks in advance for your help!
views.py
class IndexView(generic.ListView):
template_name = 'argent/index.html'
context_object_name = 'object_list'
def get_queryset(self):
return Entry.objects.all()
def get_context_data(self, **kwargs):
et = super(IndexView, self).get_context_data(**kwargs)
et['filter'] = Entry.objects.filter(date=today_date)
return et
models.py
class Entry(models.Model):
date = models.DateField(blank=True, null=True,)
euros = models.CharField(max_length=500, blank=True, null=True)
comments = models.CharField(max_length=900, blank=True, null=True)
euros_sum = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
xrate = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
dollars_sum = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
daily_savings_dollars = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
daily_savings = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
def get_absolute_url(self):
return reverse('argent:detail', kwargs={'pk': self.pk})
def item_date(self):
row_title = self.date
return row_title
class Savings(models.Model):
total_spent_euros = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
total_spent_dollars = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
total_savings = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
def get_absolute_url(self):
return reverse('argent:detail', kwargs={'pk': self.pk})
EDIT: I added more detail to this post.
Upvotes: 0
Views: 1710
Reputation: 1633
About your problem of getting two queryset result in one page :
def get_context_data(self, **kwargs):
et = super(IndexView, self).get_context_data(**kwargs)
et['entry_qs'] = Entry.objects.filter(date=today_date)
et['savings_qs'] = Savings.objects.filter(id=1)
return et
Now in your template, you have acces to this two querysets.
{{ entry_qs }}
and {{ savings_qs }}
About your error
Reverse for 'entry-update' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['tracker/entry/update/(?P[0-9]+)/$']
I suppose you write a bad template url
in your template. You use url entry-update
without an argument. So Django can't reverse this url. Because this doesn't exist. You need to pass an argument to this url
{% url 'entry-update' pk %} # replace pk by primary key of the correct object
Upvotes: 2