在我现有的视图上,我试图添加过滤器视图(通过Issue中的字段“ Sprint”-在Issue模型中作为外键工作)。下面是我的代码:
#models.py
class Sprint(models.Model):
sprint_type = models.TextField(default='Sprint', editable=False)
name = models.TextField()
goal = models.TextField()
start_date = models.DateField()
end_date = models.DateField()
project = models.ForeignKey(Project, on_delete=models.CASCADE)
class Issue(models.Model):
ISSUE_PRIORITY = (
('Critical', 'Critical'),
('High', 'High'),
('Medium', 'Medium'),
('Low', 'Low'),
)
issue_type = models.TextField(default='Issue', editable=False)
issue_id = models.AutoField(primary_key=True)
# slug = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True)
project = models.ForeignKey(Project, on_delete=models.CASCADE)
title = models.CharField(max_length=128)
description = models.TextField(null=True, blank=True)
initiative = models.ForeignKey(Initiative, on_delete=models.CASCADE, null=True, blank=True)
epic = models.ForeignKey(Epic, null=True, blank=True, on_delete=models.CASCADE)
sprint = models.ForeignKey(Sprint, on_delete=models.CASCADE, null=True, blank=True)
priority = models.CharField(max_length=8, choices=ISSUE_PRIORITY, default='M')
我正在使用django-filter库。
#filters.py
import django_filters
from .models import Issue
class BacklogFilter(django_filters.FilterSet):
sprint = django_filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Issue
fields = ['sprint']
使用基于类的视图,我定义了:
class ProjectBacklogView(DetailView):
model = Project
template_name = 'project-backlog.html'
context_object_name = 'backlog'
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return ListView.dispatch(self, request, *args, **kwargs)
def get_queryset(self):
try:
self.filter = BacklogFilter(self.request.GET, queryset=Issue.objects.all())
return self.filter
except:
return Issue.objects.all()
def get_context_data(self, **kwargs):
context = super(ProjectBacklogView, self).get_context_data(**kwargs)
context['project'] = Project.objects.all()
context['issue'] = Issue.objects.all()
# context['epic'] = Epic.objects.all()
# context['initiative'] = Initiative.objects.all()
context['sprint'] = Sprint.objects.all()
context['filter_form'] = self.filter.form.as_table
return context
在urls.py中:
path('<str:pk>/backlog/', ProjectBacklogView.as_view(), name='project-backlog'),
path('<str:pk>/backlog/(\w+)/', ProjectBacklogView.as_view(), name='project-backlog-filter'),
最后,模板:
{% extends 'base-project.html' %}
{% block content %}
<div class="container-fluid">
<div id="simpleList" class="list-group">
{% for issue in backlog.issue_set.all %}
<div class="list-group-item">
<div style="float:left;"><strong><a href="{% url 'issue-edit' issue.pk %}">{{ issue.title }}</a></strong></div>
<div style="float: right;"><a href="#" class="btn btn-success btn-circle btn-sm"><i class="fas">{{ issue.remaining_estimate }}</i></a></div>
<br /><br /><hr style="border-top: dashed 1px;"></hr>
<div style="float: left;"><small>Assignee: <strong>{{ issue.assignee }}</strong></small></div>
<div style="float: right;"><small>Initiative: <strong>{{ issue.initiative }}</strong></small></div><br />
<div style="float: left;"><small>Priority: <strong>{{ issue.priority }}</strong></small></div>
<div style="float: right;"><small>Epic: <strong>{{ issue.epic }}</strong></small></div>
</div>
{% endfor %}
</div>
<!-- Filters form-->
<div>
<form action="" method="get" class="filter-form">
<table>
{{ filter_form }}
</table>
<input type="submit" value="Szukaj" />
<a class="reset-form" href="{% url 'project-backlog' project.pk %}">Clean filters</a>
</form>
</div>
</div>
我的问题是,我在BacklogFilter中收到有关“没有属性'filter'的错误。我也担心url的路由-我的情况是我希望在当前现有的视图显示用户过滤表单上并应用选定的过滤器后,将视图上显示的元素相应地限制为选定/应用的过滤器。如何阅读?阅读大量手册,但总是找出案例,该用户具有过滤表单,并且在发送表单后,用户被重定向到完全不同的页面: (
在此先感谢您的帮助,我知道我还有很多东西要学习。