Django ListView 循环通过 class 属性 创建的信息(不是来自数据库)
Django ListView loop through info created by a class property (not from the database)
在我的 ListView 页面上,我需要能够遍历来自我的 views.py.
的状态列表
我可以在 DetailView 上完成,但我在 ListView 上遇到同样的问题。
我搜索了又搜索,但似乎无法找到解决我的具体问题的帮助。
DetailView 的工作代码:
managers.py
class ICCRequestManager:
...
@property
def statuses(self):
""" Current status of ICC request """
if not self.is_submitted:
yield "Created, not yet submitted"
if self.is_submitted and not self.legal_signoff_status:
yield "Waiting for security review"
if self.is_submitted and not self.security_signoff_status:
yield "Waiting for legal review"
if self.legal_signoff_status and self.security_signoff_status:
yield "Fully reviewed, ICC team is working"
views.py
from . import managers
...
class ICCRequestMixin:
""" Mixin for the request model """
model = models.ICCRequest
context_object_name = 'icc_request'
class ICCRequestDetailView(LoginRequiredMixin, ICCRequestMixin, DetailView):
""" View to see the current status of an ICC request """
template_name = "icc/request_status.html"
def get_context_data(self, **kwargs):
""" Add required context """
context = super().get_context_data(**kwargs)
context["icc_request_manager"] = managers.ICCRequestManager(self.object)
return context
request_status.html
<ul>
{% for value in icc_request_manager.statuses %}
<li>{{ value }}</li>
{% endfor %}
</ul>
上面按预期显示了一个简单的项目符号列表。
问题代码:
views.py
class ICCRequestListView(LoginRequiredMixin, ICCRequestMixin, ListView):
""" View to list requests """
template_name = "icc/request_list.html"
paginate_by = 10
def get_context_data(self, **kwargs):
""" Add required context """
context = super().get_context_data(**kwargs)
context["icc_request_manager"] = managers.ICCRequestManager(self.object_list)
return context
工作部分:
request_list.html
<table class="table table-striped table-bordered">
<tr>
<th scope="col">ICC #</th>
<th scope="col">Company</th>
<th scope="col">Brief Description</th>
<th scope="col">LOB</th>
<th scope="col">Requester</th>
<th scope="col">Status</th>
</tr>
{% for request in icc_request %}
<tr>
<th scope="row"><a href="{% url 'icc:request-detail' pk=request.id %}">{{ request.id }}</a></th>
<td>{{ request.company_name}}</td>
<td>{{ request.summary }}</td>
<td>{{ request.owner_area }}</td>
<td>{{ request.requested_on_behalf_of }}</td>
<!--- I want the statuses list here --->
</tr>
</table>
尝试在详情页循环点赞:
...
<!--- I want the statuses list here --->
<td>
<ul>
{% for status in request.icc_request_manager.statuses %}
<li>
{{ status }}
</li>
{% endfor %}
</ul>
</td>
{% endfor %}
</tr>
</table>
这不会给出错误,但不会显示任何内容(而 request_status.html 页面显示)。
唯一的错误是 get_context_data 上的以下 linting 错误:
"Parameters differ from overridden 'get_context_data' method."
所以我想通了。
1) ListView上的get_context_data
与它无关。没有必要。
2) 我需要从 yield 更改为返回列表:
def statuses(self):
""" Current status of icc request """
statuses = list()
if not self.is_submitted:
statuses.append("Created, not yet submitted")
if self.is_submitted and not self.legal_signoff_status:
statuses.append("Waiting for security sign off")
if self.is_submitted and not self.security_signoff_status:
statuses.append("Waiting for security review")
if self.legal_signoff_status and self.security_signoff_status:
statuses.append("Fully reviewed, ICC team is working")
return statuses
3) 为模型添加了一个方法:
@property
def get_icc_request_manager(self):
""" Get the manager for this one request """
# Needed to import managers inside the method to prevent Circular import problem
import icc.managers
mgr = icc.managers.ICCRequestManager(icc_request=self)
return mgr
4) 使用 with
:
<table class="table table-striped table-bordered">
<tr>
<th scope="col">ICC #</th>
<th scope="col">Company</th>
<th scope="col">Brief Description</th>
<th scope="col">LOB</th>
<th scope="col">Requester</th>
<th scope="col">Status</th>
</tr>
{% for request in icc_request %}
{% with request_manager=request.get_icc_request_manager %}
<tr>
<th scope="row"><a href="{% url 'icc:request-detail' pk=request.id %}">{{ request.id }}</a></th>
<td>{{ request.company_name}}</td>
<td>{{ request.summary }}</td>
<td>{{ request.owner_area }}</td>
<td>
{% if request.requested_on_behalf_of %}
{{ request.requested_on_behalf_of }}
{% elif request.requested_by %}
{{ request.requested_by }}
{% else %}
{% endif %}
</td>
<td>
<ul>
{% for status in request_manager.statuses %}
<li>
{{ status }} test
</li>
{% endfor %}
</ul>
</td>
{% endwith %}
{% endfor %}
</tr>
</table>
在我的 ListView 页面上,我需要能够遍历来自我的 views.py.
的状态列表我可以在 DetailView 上完成,但我在 ListView 上遇到同样的问题。
我搜索了又搜索,但似乎无法找到解决我的具体问题的帮助。
DetailView 的工作代码:
managers.py
class ICCRequestManager:
...
@property
def statuses(self):
""" Current status of ICC request """
if not self.is_submitted:
yield "Created, not yet submitted"
if self.is_submitted and not self.legal_signoff_status:
yield "Waiting for security review"
if self.is_submitted and not self.security_signoff_status:
yield "Waiting for legal review"
if self.legal_signoff_status and self.security_signoff_status:
yield "Fully reviewed, ICC team is working"
views.py
from . import managers
...
class ICCRequestMixin:
""" Mixin for the request model """
model = models.ICCRequest
context_object_name = 'icc_request'
class ICCRequestDetailView(LoginRequiredMixin, ICCRequestMixin, DetailView):
""" View to see the current status of an ICC request """
template_name = "icc/request_status.html"
def get_context_data(self, **kwargs):
""" Add required context """
context = super().get_context_data(**kwargs)
context["icc_request_manager"] = managers.ICCRequestManager(self.object)
return context
request_status.html
<ul>
{% for value in icc_request_manager.statuses %}
<li>{{ value }}</li>
{% endfor %}
</ul>
上面按预期显示了一个简单的项目符号列表。
问题代码:
views.py
class ICCRequestListView(LoginRequiredMixin, ICCRequestMixin, ListView):
""" View to list requests """
template_name = "icc/request_list.html"
paginate_by = 10
def get_context_data(self, **kwargs):
""" Add required context """
context = super().get_context_data(**kwargs)
context["icc_request_manager"] = managers.ICCRequestManager(self.object_list)
return context
工作部分: request_list.html
<table class="table table-striped table-bordered">
<tr>
<th scope="col">ICC #</th>
<th scope="col">Company</th>
<th scope="col">Brief Description</th>
<th scope="col">LOB</th>
<th scope="col">Requester</th>
<th scope="col">Status</th>
</tr>
{% for request in icc_request %}
<tr>
<th scope="row"><a href="{% url 'icc:request-detail' pk=request.id %}">{{ request.id }}</a></th>
<td>{{ request.company_name}}</td>
<td>{{ request.summary }}</td>
<td>{{ request.owner_area }}</td>
<td>{{ request.requested_on_behalf_of }}</td>
<!--- I want the statuses list here --->
</tr>
</table>
尝试在详情页循环点赞:
...
<!--- I want the statuses list here --->
<td>
<ul>
{% for status in request.icc_request_manager.statuses %}
<li>
{{ status }}
</li>
{% endfor %}
</ul>
</td>
{% endfor %}
</tr>
</table>
这不会给出错误,但不会显示任何内容(而 request_status.html 页面显示)。
唯一的错误是 get_context_data 上的以下 linting 错误:
"Parameters differ from overridden 'get_context_data' method."
所以我想通了。
1) ListView上的get_context_data
与它无关。没有必要。
2) 我需要从 yield 更改为返回列表:
def statuses(self):
""" Current status of icc request """
statuses = list()
if not self.is_submitted:
statuses.append("Created, not yet submitted")
if self.is_submitted and not self.legal_signoff_status:
statuses.append("Waiting for security sign off")
if self.is_submitted and not self.security_signoff_status:
statuses.append("Waiting for security review")
if self.legal_signoff_status and self.security_signoff_status:
statuses.append("Fully reviewed, ICC team is working")
return statuses
3) 为模型添加了一个方法:
@property
def get_icc_request_manager(self):
""" Get the manager for this one request """
# Needed to import managers inside the method to prevent Circular import problem
import icc.managers
mgr = icc.managers.ICCRequestManager(icc_request=self)
return mgr
4) 使用 with
:
<table class="table table-striped table-bordered">
<tr>
<th scope="col">ICC #</th>
<th scope="col">Company</th>
<th scope="col">Brief Description</th>
<th scope="col">LOB</th>
<th scope="col">Requester</th>
<th scope="col">Status</th>
</tr>
{% for request in icc_request %}
{% with request_manager=request.get_icc_request_manager %}
<tr>
<th scope="row"><a href="{% url 'icc:request-detail' pk=request.id %}">{{ request.id }}</a></th>
<td>{{ request.company_name}}</td>
<td>{{ request.summary }}</td>
<td>{{ request.owner_area }}</td>
<td>
{% if request.requested_on_behalf_of %}
{{ request.requested_on_behalf_of }}
{% elif request.requested_by %}
{{ request.requested_by }}
{% else %}
{% endif %}
</td>
<td>
<ul>
{% for status in request_manager.statuses %}
<li>
{{ status }} test
</li>
{% endfor %}
</ul>
</td>
{% endwith %}
{% endfor %}
</tr>
</table>