将查询集传递给模板 [英] Passing a Query Set to a Template
问题描述
models.Manager
创建一个查询集,它计算任务的数量
一个特定的 project
has。我还在 views.py
中创建了两个变量,以过滤结果以提供更具体的输出。在我的模板中,我想要显示项目X有11个任务打开
和项目Y有5个任务打开
应该注意的是,我可以通过使用 TaskCount
经理的结果访问 c $ c> {{project.tasks}} ,但我真正感兴趣的是访问我的模板中的 open_set
变量。 p>
models.py
class TaskCount(models.Manager):
def get_query_set(self):
返回超级(TaskCount,self).get_query_set()。annotate(tasks = Count('task'))
类项目(models.Model):
#模型字段
#某些字段
#模型管理器
with_tasks = TaskCount()
views.py
$ b $
$ b $查询
open_set = P roject.with_tasks.filter(task__status = 0)
closed_set = Project.with_tasks.filter(task__status = 1)
list_project.html
{%for project in object_list%}
< tr>
<! - works - >
< td>< a href ={{project.get_absolute_url}}> {{project.name}}< / a>< / td>
<! - works - >
< td> {{project.created_by}}< / td>
<! - 不起作用 - >
< td> {{open_set}}< / td>
<! - 不起作用 - >
< td> {{project.closed_set}}< / td>
< / tr>
{%endfor%}
你应该不要把那些查询放在类级别上:ListView不了解它们,也不会在任何地方传递它们。相反,你应该覆盖 get_context_data
并从那里返回。
class ListProject (ListView):
...
def get_context_data(self,* args,** kwargs):
context = super(ListProject,self).get_context_data(* args,** kwargs)
上下文['open_set'] = Project.with_tasks.filter(task__status = 0)
上下文['closed_set'] = Project.with_tasks.filter(task__status = 1)
返回上下文
I have a template that lists out all of the projects that have been created. I've used the models.Manager
to create a query set that counts the number of tasks
a particular project
has. I've also created two variables in views.py
that filter the results to provide more specific output. Within my template I want to be able to display Project X has 11 tasks open
and Project Y has 5 tasks open
(in a tabular format).
It should be noted that I can access the TaskCount
manager's result by using {{ project.tasks }}
but what I'm really interested in is accessing the open_set
variable in my template.
models.py
class TaskCount(models.Manager):
def get_query_set(self):
return super(TaskCount, self).get_query_set().annotate(tasks=Count('task'))
class Project(models.Model):
# Model Fields
# some fields
# Model Managers
with_tasks = TaskCount()
views.py
class ListProject(ListView):
model = Project
template_name = 'list_project.html'
# Queries
open_set = Project.with_tasks.filter(task__status=0)
closed_set = Project.with_tasks.filter(task__status=1)
list_project.html
{% for project in object_list %}
<tr>
<!-- works -->
<td><a href="{{ project.get_absolute_url }}">{{ project.name }}</a></td>
<!-- works -->
<td>{{ project.created_by }}</td>
<!-- doesn't work -->
<td>{{ open_set }}</td>
<!-- doesn't work -->
<td>{{ project.closed_set }}</td>
</tr>
{% endfor %}
You shouldn't put those queries there at the class level: the ListView doesn't know about them and won't pass them anywhere. Instead you should override get_context_data
and return them from there.
class ListProject(ListView):
...
def get_context_data(self, *args, **kwargs):
context = super(ListProject, self).get_context_data(*args, **kwargs)
context['open_set'] = Project.with_tasks.filter(task__status=0)
context['closed_set'] = Project.with_tasks.filter(task__status=1)
return context
这篇关于将查询集传递给模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!