我正在处理一个项目,我遇到了djago表单模板的问题,它只返回表单的一部分。
所以发生的事情是: 我有一个ModelForm包含两个字段,金额和描述。在视图中,我循环浏览用户列表。对于循环中的每个用户,它使用用户名前缀创建一个表单,然后将表单列表传递到html模板中。
我遇到的问题是所有表单及其前缀都传递给html tempalte,但是当提交时,只传递最后一个表单以进行处理。我在下面有一个示例以及与此问题相关的文件信息。
请查看问题底部的更新部分
以下是在显示html模板之前在视图中创建的传入表单:
omarform
<tr><th><label for="id_omar-amount">Amount:</label></th><td><input id="id_omar-amount" name="omar-amount" step="0.01" type="number" value="0.0" /></td></tr>
<tr><th><label for="id_omar-description">Description:</label></th><td><input id="id_omar-description" maxlength="250" name="omar-description" type="text" /></td></tr>
haniform
<tr><th><label for="id_hani-amount">Amount:</label></th><td><input id="id_hani-amount" name="hani-amount" step="0.01" type="number" value="0.0" /></td></tr>
<tr><th><label for="id_hani-description">Description:</label></th><td><input id="id_hani-description" maxlength="250" name="hani-description" type="text" /></td></tr>
ranaform
<tr><th><label for="id_rana-amount">Amount:</label></th><td><input id="id_rana-amount" name="rana-amount" step="0.01" type="number" value="0.0" /></td></tr>
<tr><th><label for="id_rana-description">Description:</label></th><td><input id="id_rana-description" maxlength="250" name="rana-description" type="text" /></td></tr>
以下是正确显示的内容:
fill out the form below
Amount:
0.0
Description:
Amount:
0.0
Description:
Amount:
0.0
Description:
Tax:
Tip:
submit
但是这是我尝试提交表单时返回的内容:
Request information
GET
No GET data
POST
Variable Value
rana-amount '0.0'
rana-description ''
tax ''
tip ''
submit 'submit'
以下是代码:
HTML:
{% extends "base.html" %}
{% block content %}
<h1>Add members to {{record.name}}</h1>
{% if message %}
<p>{{message}}</p>
{% endif %}
<form action="." method="POST">
{% csrf_token %}
{{ form.as_p }}
<p>Tax: <input type="text" name="tax" value=""></p>
<p>Tip: <input type="text" name="tip" value=""></p>
<input type="submit" name="submit" value="submit">
</form>
{% endblock %}
的观点:
def addTransaction(request, groupId, recordId):
user = User.objects.get(username='omar')
group = Group.objects.get(id=groupId)
record = Record.objects.get(id=recordId)
transactions = Transaction.objects.all()
if request.method == 'POST':
if record.split == 1:
form = EvenSplitTransactionForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
amount = cd['amount']
description = cd['description']
split_amount = SplitEven(record, amount)
for trans in transactions:
if trans.record.id == record.id:
trans.description = description
trans.amount = split_amount
trans.save()
return redirect('accounts')
if record.split == 2:
form = IndividualSplitTransactionForm(requesst.POST)
if form.is_valid():
cd = form.cleaned_data
for trans in transactions:
if trans.record.id == record.id:
amount = 'desiatnace'
# even_form = EvenSplitTransactionForm(request.POST)
# if even_form.is_valid():
# even_cd = even_form.cleaned_data
# amount = even_cd['amount']
# description = even_cd['description']
# if record.split == 1:
# split_amount = SplitEven(record, amount)
# for trans in transactions:
# if trans.record.id == record.id:
# trans.description = description
# trans.amount = split_amount
# trans.save()
# if record.split == 2:
# splits = amount
return redirect('accounts')
else:
if record.split == 1:
form = EvenSplitTransactionForm()
message = 'fill out the form below'
parameters = {
'record':record,
'form':form,
'message':message,
'transactions':transactions,
}
return render(request, 'tabs/add_even_transactions.html', parameters)
if record.split == 2:
for trans in transactions:
if trans.record.id == record.id:
formName = trans.user.username + 'form'
print(formName)
form = IndividualSplitTransactionForm(prefix=trans.user.username)
print(form)
message = 'fill out the form below'
parameters = {
'record':record,
'form':form,
'message':message,
'transactions':transactions,
}
return render(request, 'tabs/add_individual_transaction.html', parameters)
形式:
class IndividualSplitTransactionForm(forms.ModelForm):
class Meta:
model = Transaction
fields = ['amount', 'description']
已更新
我在模板文件中,我删除了一些评论代码,我发现只有我传入模板的三个中的最后一个形式是beign显示并由模板吹嘘。所以我意识到我遇到的真正问题是我需要传递三种形式,只有一种形式被传递。我需要弄清楚如何将所有三个表单合并为一个并传递它,或者在for循环中使用不同的名称单独传递所有三个表单。
这是我在通过之前的形式......
if record.split == 2:
for trans in transactions:
if trans.record.id == record.id:
formName = trans.user.username + 'form'
form = IndividualSplitTransactionForm(prefix=trans.user.username)
print(form)
message = 'fill out the form below'
parameters = {
'record':record,
'form':form,
'message':message,
'transactions':transactions,
}
return render(request, 'tabs/add_individual_transaction.html', parameters)
答案 0 :(得分:0)
我知道您有多个&#39;,&#39;描述&#39;一种形式的字段。
例如,你有Sarah,Pill,Kelly。您的表单中有三个字段,如下所示。<form action="." method="post">{% csrf_token %}
## Sarah section
<input type="text" name="amount">
<input type="text" name="description">
## Pill section
<input type="text" name="amount">
<input type="text" name="description">
## Kelly section
<input type="text" name="amount">
<input type="text" name="description">
<button name="submit">Submit</button>
</form>
我假设您使用相同的名称&#39;金额&#39;并且&#39; desciption&#39;因此,如果您从request.POST.get('amount', None)
获取数据,您将只获得最后的数据。试试request.POST.getlist('amount')
。
您可以使用print(request.POST);