我需要做的是,我需要转到一个列表视图到另一个列表视图。但是我需要使用单击的链接来过滤后面的列表视图。那么filter属性是什么?
我有一个带有批次(sbtc)的学生模型,我需要使用选定批次的过滤器传递稍后的列表视图。
@method_decorator(login_required, name='dispatch')
class FindStudent(ListView):
template_name = 'Dashboard/findStudent.html'
model = Student
fields = ['sbtc']
def get_queryset(self):
batch = Student.objects.values_list('sbtc').distinct()
return batch
@method_decorator(login_required, name='dispatch')
class FindStudentdetail(ListView):
template_name = 'Dashboard/findStudentdetail.html'
model = Student
fields = ['all']
def get_queryset(self):
student = Student.objects.filter(sbtc=#here what will be)
return student
答案 0 :(得分:0)
在模板中,您只需添加指向findStudentDetail
模板的链接即可;这是docs的修改示例:
<h1>Articles</h1>
<ul>
{% for article in object_list %}
<li>{{ article.pub_date|date }} - {{ article.headline }}</li>\
<a href="/articleDetail.html?smbc={{article.smbc}}">View Detail</a>
{% empty %}
<li>No articles yet.</li>
{% endfor %}
</ul>
然后,在响应视图中,您只需使用kwargs
即可获取GET参数:
class FindStudentdetail(ListView):
template_name = 'Dashboard/findStudentdetail.html'
model = Student
fields = ['all']
def get_queryset(self):
student = Student.objects.filter(sbtc=self.kwargs['sbtc'])
return student
答案 1 :(得分:0)
def get_queryset(self):
student = Student.objects.filter(sbtc=self.kwargs['pk'])
return student