我正在构建一个web应用程序,我试图将queryset作为Django 2.0中的变量从基于类的视图传递到模板。但是变量没有显示在http://localhost:8000/joblist/。其他文本会显示在这个url路由上,但是queryset对象不会作为变量显示在页面上。这个模型.py数据库运行良好,数据库中填充了~10k条记录。我错过了什么?我还想知道基于类的视图相对于基于函数的视图的优点。谢谢您。
我的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'
我的模板:joblist.html
您已经将context_object_name设置为“job_title”,因此您应该在模板中使用这个名称,而不是默认的“object_list”。工作清单似乎是一个更合理的名字。在
您可以尝试使用以下代码来迭代list中的context_object_name。谢谢
相关问题 更多 >
编程相关推荐