1

I am building a web app.I am trying to pass a queryset from a class based view to templates as variables in Django 2.0. But the variables were not displayed at http://localhost:8000/joblist/. Other text are displayed on this url routing, but the queryset objects are not displayed as variables on the page. The models.py and the database work fine, and the database is populated with ~10k records. What am i missing? I would also like to know the advantages of class based views over function based views in simple words. Thank you. My views.py:

from django.http import HttpResponse
from django.template.response import TemplateResponse
from django.views.generic import ListView,DetailView
from joblist.models import Jobs

def index(request):
    context = {}
    html = TemplateResponse(request,'welcome.html',context)
    return HttpResponse(html.render())

class JobList(ListView):
    model=Job
    context_object_name = 'job_title'
    queryset=Job.objects.all()
    template_name = 'job_list.html'

class JobDetail(DetailView):

    model = Job
    context_object_name = 'job_detail'
    queryset = Job.objects.all()
    template_name = 'job_detail.html'

My template: joblist.html

{% extends 'welcome.html' %}
<h1> Job Title </h1>

<ul>
    {% for job in object_list %}
        <li>{{ job_title }}</li>
    {% endfor %}
</ul>
3
  • The context variable for the list was set to context_object_name = 'job_title'. Use {% for job in job_title %} in the template to iterate over the queryset. Commented Feb 7, 2018 at 19:41
  • I used job_title as a variable in the template as you suggested, or i don't know where to place it? Commented Feb 7, 2018 at 19:45
  • The template has no context variable for the name object_list - you renamed the variable to job_title. No object_list no iteration - no {{job_title}}. Commented Feb 7, 2018 at 19:47

3 Answers 3

3

You have set the context_object_name to "job_title", so thats what you should use in the template, not "object_list" which is the default. job_list would seem a more sensible name.

<ul>
    {% for job in job_title %}
        <li>{{ job }}</li>
    {% endfor %} 
</ul>
Sign up to request clarification or add additional context in comments.

1 Comment

job_list does indeed make more sense. In fact, as model has already been set on the view, job_list will be included in the context by default (object_list will also be present). See MultipleObjectMixin.get_context_object_name.
1

You may try this code for iterating context_object_name in list. Thanks

 <ul>
        {% for job in job_title %}
            <li>{{ job.model_field_name }}</li>
        {% endfor %} 
 </ul>

1 Comment

<ul> {% for job in job_title %} <li>{{ job.model_field_name }}</li> {% endfor %} </ul>
0

You have to modify your code as below:

<ul>
  {% for job in job_title %}
    <li>{{ job.model_field_name }}</li>
  {% endfor %}
</ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.