gpt4 book ai didi

python - 如何在 Django 中使用过滤后的数据填充表

转载 作者:太空宇宙 更新时间:2023-11-03 20:41:18 25 4
gpt4 key购买 nike

我正在尝试使用 url 参数从 list 模型中过滤出一组数据来填充表。

我相信我遇到的问题出在 Views.py 行

 manifests = Manifests.objects.all().filter(reference=reference_id)

模型.py

class Manifests(models.Model):

reference = models.ForeignKey(Orders)
cases = models.IntegerField()
description = models.CharField(max_length=1000)
count = models.IntegerField()

def __str__(self):
return self.description

网址.py

url(r'^add_manifest/(?P<reference_id>\d+)/$', add_manifest, name='add_manifest'),

View .py

def add_manifest(request, reference_id):

if request.method == "POST":
form = CreateManifestForm(request.POST)

if form.is_valid():
instance = form.save(commit=False)
try:
order = Orders.objects.get(id=reference_id)
except Orders.DoesNotExist:
pass
instance.reference = order
instance.save()
return redirect('add_manifest', reference_id=reference_id)

form = CreateManifestForm()
#manifests = Manifests.objects.all()
manifests = Manifests.objects.all().filter(reference=reference_id)

context = {
'form': form,
'reference_id': reference_id,
'manifests' : manifests,
}

return render(request, 'add_manifest.html', context)

模板(add_manifest.html)

  {% extends 'base.html' %}

{% block body %}

<div class="container">
<form method="POST">

<br>
<br>
<br>
{% csrf_token %}

<div class="column">
<label for="form.reference">Reference ID: </label><br>
<input type="text" value="{{ reference_id }}">
<br>
</div>
<div class="description">
<div class="column">
<label for="form.description">Description: </label>
<br>
{{ form.description}}
</div>
</div>
<div class="column">
<label for="form.cases">Cases: </label>
<br>
{{ form.cases }}
<br>
</div>
<div class="column">
<label for="form.count">Count: </label>
<br>
{{ form.count }}
<br>
<br>
</div>
<br>
<br>


<button type="submit" name="add_mani" style="border-color: #7395AE;">Add Line</button>
</form>


<br>
<h4>Manifest</h4>

<div class="table-responsive">
<table class="table table-striped table-bordered manifest_table" cellspacing="0" style="width="100%">

<thead>
<tr>
<th></th>
<th style="width:10%;">Ref ID</th>
<th style="width:10%;">Cases</th>
<th style="width:60%;">Description</th>
<th style="width:10%;">Count</th>
</tr>
</thead>

<tbody>

{% for manifests in manifests %}

<tr>
<td>
<a href="{% url 'edit_manifest_browse' manifests.pk %}" class="btn btn-default btn-sm" role="button">Edit</a>
</td>
<td>{{ manifests.reference }}</td>
<td>{{ manifests.cases }}</td>
<td>{{ manifests.description}}</td>
<td>{{ manifests.count}}</td>
</tr>

{% endfor %}

</tbody>
</table>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary" name="button" align="right">Subit Manifest</button>
</div>

</div>

我希望表格仅显示 list 模型中的引用= URL 中的reference_id 的行。目前它无法正常工作,该表只是空的。

最佳答案

像这样更改 for 循环变量名称:

{% for manifest in manifests %}
<tr>
<td>
<a href="{% url 'edit_manifest_browse' manifests.pk %}" class="btn btn-default btn-sm" role="button">Edit</a>
</td>
<td>{{ manifest.reference }}</td>
<td>{{ manifest.cases }}</td>
<td>{{ manifest.description}}</td>
<td>{{ manifest.count}}</td>
</tr>

{% endfor %}

如果 reference_id 存在任何 Manifest 对象,那么它将在模板中呈现它们。

更新

您的表单可能未通过验证。所以渲染表单错误是个好主意:

# view
def add_manifest(request, reference_id):
form = CreateManifestForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
instance = form.save(commit=False)
try:
order = Orders.objects.get(id=reference_id)
instance.reference = order
except Orders.DoesNotExist:
pass
instance.save()
manifests = Manifests.objects.all().filter(reference=reference_id)

context = {
'form': form,
'reference_id': reference_id,
'manifests' : manifests,
}

return render(request, 'add_manifest.html', context)

并更新模板以显示模板中的错误:

{% if form.errors %}
{% for field in form %}
{% for error in field.errors %}
<div>
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endfor %}
{% for error in form.non_field_errors %}
<div>
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
{% endif %}

关于python - 如何在 Django 中使用过滤后的数据填充表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56844928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com