Django中同一视图中的两个独立查询列表



我希望在同一页面上有两个单独的查询列表,但使用不同的过滤器。必须播放第一个列表,并暂停第二个列表。这是我在views.py文件中想到的,但它抛出了一个错误,如下所示:UnboundLocalError/分配前引用的局部变量"formset_paused">

def home_view(request):
#campaigns in progress
queryset = Campaign.objects.filter(is_active=True, completion_percent__lt=100)
if request.method == "POST":
form_type = request.POST.get('id')
if form_type == 'campaign_status':
formset = CampaignStatusFormSet(
request.POST, request.FILES,
queryset=queryset,
)
formset.save()
else:
formset = CampaignStatusFormSet(queryset=queryset)
campaigns_and_forms = list(zip(queryset, formset))

#paused campaigns
queryset_paused = Campaign.objects.filter(is_active=False, completion_percent__lt=100)
if request.method == "POST":
form_type_paused = request.POST.get('id_paused')
if form_type_paused == 'campaign_status_paused':
formset_paused = CampaignStatusFormSet(
request.POST, request.FILES,
queryset=queryset_paused,
)
formset_paused.save()
else:
formset_paused = CampaignStatusFormSet(queryset=queryset_paused)
paused_campaigns_and_forms = list(zip(queryset_paused, formset_paused))
context = {
'formset': formset,
'formset_paused': formset_paused,
'campaigns_and_forms': campaigns_and_forms,
'paused_campaigns_and_forms': paused_campaigns_and_forms,
}
return render(request, 'campaigns_in_progress.html', context)

以下是我的模板中的表,我在其中列出了这两个列表。

<table class="table table-striped table-hover table-bright table-bordered align-middle">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Nazwa</th>
<th scope="col">Temat</th>
<th scope="col">Nadawca</th>
<th scope="col">Procent Realizacji</th>
<th scope="col">Start Kampani</th>
<th scope="col">Stan</th>
</tr>
</thead>
<tbody>
<form method="post" id="campaign_status"> {% csrf_token %}
<input type='hidden' value='campaign_status' name='id'>
{{ formset.management_form }}
{% for campaign, form in campaigns_and_forms %}
<tr>
<td>{{ campaign.campaign_id }}</td>
<td>{{ campaign.name }}</td>
<td>{{ campaign.topic }}</td>
<td>{{ campaign.sender }}</td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: {{campaign.completion_percent}}%;" aria-valuenow="{{campaign.completion_percent}}" aria-valuemin="0" aria-valuemax="100">{{campaign.completion_percent}}%</div>
</div>
</td>
<td>{{ campaign.start_date }}</td>
<td>{{ form.as_p }}</td>
</tr>
{% endfor %}
</form>
</tbody>
</table>
<h4>Kampanie zatrzymane</h4>
<table class="table table-striped table-hover table-bright table-bordered align-middle">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Nazwa</th>
<th scope="col">Temat</th>
<th scope="col">Nadawca</th>
<th scope="col">Procent Realizacji</th>
<th scope="col">Start Kampani</th>
<th scope="col">Stan</th>
</tr>
</thead>
<tbody>
<form method="post" id="campaign_status_paused"> {% csrf_token %}
<input type='hidden' value='campaign_status_paused' name='id_paused'>
{{ formset_paused.management_form }}
{% for campaign, form in paused_campaigns_and_forms %}
<tr>
<td>{{ campaign.campaign_id }}</td>
<td>{{ campaign.name }}</td>
<td>{{ campaign.topic }}</td>
<td>{{ campaign.sender }}</td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: {{campaign.completion_percent}}%;" aria-valuenow="{{campaign.completion_percent}}" aria-valuemin="0" aria-valuemax="100">{{campaign.completion_percent}}%</div>
</div>
</td>
<td>{{ campaign.start_date }}</td>
<td>{{ form.as_p }}</td>
</tr>
{% endfor %}
</form>
</tbody>
</table>

在添加第二个查询和第二个表之前,一切都正常。

所以基本上我想要的是当is_active过滤器等于True时,活动应该在第一个列表中。如果它是False,那么它应该跳到另一个。有人能帮我吗?

如果request.method == "POST"而不是form_type_paused != 'campaign_status_paused',则永远不会分配formset_paused变量。但你尝试在这里访问它:

...
paused_campaigns_and_forms = list(zip(queryset_paused, formset_paused))
...

这就是错误的来源。

最新更新