我有一個Django視圖,我試圖提交和處理。窗體不循環顯示當我試圖DJANGO
我有一個ModelForm,我試圖通過HTML模板。現在使用表單,我想遍歷它,每次我循環遍歷它時,我都想爲字段名稱添加一個前綴,以便我可以更輕鬆地處理它。
我現在擁有的是一個視圖,它可以正確循環每個項目並添加前綴,但是當它傳遞並顯示到HTML模板時,我只會得到最後一個模板的結果表格。我不知道如何實現這一目標,以及我需要做些什麼來實現它。
這裏是我的觀點:
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(request.POST)
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:
message = 'fill out the form below'
for trans in transactions:
if trans.record.id == record.id:
print(trans.user.username)
form = IndividualSplitTransactionForm(prefix=trans.user.username)
print(form)
parameters = {
'record':record,
'form':form,
'message':message,
'transactions':transactions,
}
return render(request, 'tabs/add_individual_transaction.html', parameters)
這裏作爲形式正在經歷的循環是什麼顯示。
hani
<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>
rana
<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>
amer
<tr><th><label for="id_amer-amount">Amount:</label></th><td><input id="id_amer-amount" name="amer-amount" step="0.01" type="number" value="0.0" /></td></tr>
<tr><th><label for="id_amer-description">Description:</label></th><td><input id="id_amer-description" maxlength="250" name="amer-description" type="text" /></td></tr>
,這裏是正在顯示的HTML:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link href="/static/css/blog.css" rel="stylesheet">
</head>
<body>
<div id="content">
<h1>Add members to </h1>
<p>fill out the form below</p>
<form action="." method="POST">
<input type='hidden' name='csrfmiddlewaretoken' value='Gnb1o5rbKqkoegYCX5biYwtZgK3XMXBa' />
<p><label for="id_amer-amount">Amount:</label> <input id="id_amer-amount" name="amer-amount" step="0.01" type="number" value="0.0" /></p>
<p><label for="id_amer-description">Description:</label> <input id="id_amer-description" maxlength="250" name="amer-description" type="text" /></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>
</div>
</body>
</html>
如何,我可以得到所有三個正在創造傳遞給HTML模板的形式?如果可能的話,我想獲取所有處理和分配在views.py文件中完成,而不是html文件。
UPDATE
這是我有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 %}
第二個片段是模板代碼嗎?我看不到參數在哪裏使用。 – Park
這就是當我通過每個週期打印表單時顯示的內容... –
你可以發佈你的模板代碼嗎? – Park