I have a class-based view that I use to obtain a queryset and pass to django-tables2 which renders the result. That aspect all works fine. I am trying to pass a record instance from a different queryset to the template, so I can display information above the table django-tables2 produces.
Upon searching, it seems the 'right' way to do so is via the get_context_data
method. However when I attempt do add this method to my view, simply obtaining the queryset and returning it, it produces an error Expected table or queryset, not str
. I isolated this to being due to {% render_table table %}
in my template. Without that, I can access my 'team' object as intended.
Why is this happening? The qs queryset was being passed fine to django-tables2 before I added my get_context_data method. Does the qs queryset have to be returned via get_context_data as well? If so, why?
This is my attempt:
class myteam(LoginRequiredMixin, SingleTableView):
def get_queryset(self):
qs = Contestant.objects.filter(assigned_team=self.request.user.contestant.assigned_team)
qs = qs.exclude(id=self.request.user.contestant.id)
return qs
def get_template_names(self):
return 'xgames/viewteam.html'
def get_table_class(self):
return TeamsTable
def get_context_data(self):
team = Team.objects.get(id=self.request.user.contestant.assigned_team.id)
return {"team": team}
seems like you forgot to call the super() method
class myteam(LoginRequiredMixin, SingleTableView):
# Rest of the code
def get_context_data(self):
context = super().get_context_data()
context["team"] = Team.objects.get(
id=self.request.user.contestant.assigned_team.id
)
return context