Reputation: 123
I've built a for-loop in my HTML template and its nearly working. The issue I'm having is its listing Matches that are apart of a different tour.
I think the way to fix this is adding a filter to the view that basically says "only pull in the matches to do with this tour" which is what I've tried to do below in the Match.objects.filter() but it isnt working and I'm not sure why.
class CricketCalendar(generic.ListView):
template_name="monthly_view/cricket-monthly-view.html"
context_object_name='cricket_monthly_view'
queryset = CricketMonthlyView.objects.all()
def get_context_data(self, **kwargs):
context = super(CricketCalendar, self).get_context_data(**kwargs)
context['Tour'] = Tour.objects.all()
context['Match'] = Match.objects.filter(tour=self.request.Tour)
return context
I have also tried the following and neither worked:
self.kwargs['pk']
self.kwargs['Tour']
Edit, forgot to add the following:
Monthly View models.py:
class CricketMonthlyView(models.Model):
tour = models.ForeignKey('cricket.Tour', on_delete=models.CASCADE,
related_name='tour_name')
match_id = models.ForeignKey('cricket.Match', on_delete=models.CASCADE)
and the URLs.py:
url(r'^monthly-view/$', monthly_view.CricketCalendar.as_view(), name='cricket-monthly'),
Cricket models.py:
class Tour(models.Model):
name = models.CharField(max_length=200)
tier_level = models.ForeignKey('sports.Tier')
country = CountryField()
class Match(models.Model):
tour = models.ForeignKey('Tour', on_delete=models.CASCADE)
And the HTML Template:
{% for match_info in cricket_monthly_view %}
{% for tour in Tour %}
<ul>
<li>{{tour.name}}</li>
</ul>
{% for match in Match %}
<ul>
<li>{{match.home_team}}</li>
<li>{{match.away_team}}</li>
</ul>
{% endfor %}
{% endfor %}
{% endfor %}
Upvotes: 0
Views: 640
Reputation: 1824
This is a great place for adding a break-point. You pretty much want to know the fields on your context, and on self. Add import pdb; pdb.set_trace()
in get_context_data
, and you'll be able to see the fields on your objects. Use dir(obj)
and obj.keys()
in order to see all the fields on something.
Alternatively, if you have access to the tour object in your context variable, in your template you can get its matching Matches with tour.match_set.all
Also, be careful about naming the context variable Tour
with a capital T, because that's the name of your model.
Upvotes: 1