Trouble with Django Generic List view

11 views
Skip to first unread message

Sam Hefer

unread,
Jan 1, 2020, 5:02:13 PM1/1/20
to Django users
Hi,

I'm having some issues with displaying a filtered list in a template.

My View:
class MyJobs(ListView):
model = Job
template_name = 'jobs/my_job_list.html'
context_object_name = 'jobs'

def get_queryset(self, *args, **kwargs):
jobs = Job.objects.filter(company_name__owner=self.request.user)
return jobs, print(jobs)

My Template:
<ul>
{% for job in jobs %}
<li>{{ job.company_name }}</li>
{% endfor %}
</ul>

The print function on jobs shows the correct list for the logged in user, but for some reason, in the template the view only lists two objects (for all users) and the field reference doesn't show anything (jobs.company_name is always empty).

Have I missed something?

Thanks,
Sam


Jason

unread,
Jan 1, 2020, 5:08:53 PM1/1/20
to Django users
when you do 

return jobs, print(jobs)


you're returning a tuple from the method, which is not what is expected.  Remove the print statement from that line

Sam Hefer

unread,
Jan 1, 2020, 5:29:45 PM1/1/20
to Django users
Ah! Okay.

Thank you!
Reply all
Reply to author
Forward
0 new messages