0
我在Django Crud應用程序中創建了一個添加/更新表單。數據模型是這樣的:以表格的形式在DJango CRUD應用程序中顯示初始值
class Proposal(models.Model):
org = models.CharField(max_length=200)
country = models.CharField(max_length=100)
recdate = models.DateField(blank=True, null=True)
subdate = models.DateField(blank=True, null=True)
status = models.CharField(max_length=20)
和views.py我有以下的添加和更新代碼:
地址
def proposal_create(request, template_name='proposal/proposal_form.html'):
form = ProposalForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('list')
return render(request, template_name, {'form':form})
UPDATE
def proposal_update(request, pk, template_name='proposal/proposal_form.html'):
server = get_object_or_404(Proposal, pk=pk)
form = ProposalForm(request.POST or None, instance=server)
if form.is_valid():
form.save()
return redirect('list')
return render(request, template_name, {'form':form})
AND fina LLY我有我的模板我有forms.py是這樣的:
<div align="center" style=" margin-top: 150; padding-top: 30; padding-bottom: 20; text-align: right; border-style: double; width: 50%; float: center; margin-left: auto; margin-right: auto; background-color: grey; color: black; border-color: white; border-width: 5px; box-shadow: 10px 10px 5px #888888;">
<form method="post">{% csrf_token %}
<div align="center" style="margin-right: auto; margin-left: auto; align-content: center; width: 80%; vertical-align: middle;">
<table class="table table-bordered">
<tr>
<td valign="middle" width="30%"><label for="{{ form.org.id_for_label }}">Organization:</label></td>
<td valign="middle" width="70%"><input type="text" name="org" class="form-control" form="{form.org}"></td>
</tr>
<tr>
<td valign="middle" width="30%"><label for="{{ form.country.id_for_label }}">Country:</label></td>
<td valign="middle" width="70%"><input type="text" name="org" class="form-control" form="{form.country}"></td>
</tr>
<tr>
<td valign="middle" width="30%"><label for="{{ form.recdate.id_for_label }}">Received Date:</label></td>
<td valign="middle" width="70%"><input type="Date" name="org" class="form-control" form="{form.recdate}"></td>
</tr>
<tr>
<td valign="middle" width="30%"><label for="{{ form.subdate.id_for_label }}">Submission Deadline:</label></td>
<td valign="middle" width="70%"><input type="Date" name="org" class="form-control" form="{form.subdate}"></td>
</tr>
<tr>
<td valign="middle" width="30%"><label for="{{ form.status.id_for_label }}">Status:</label></td>
<td valign="middle" width="70%"><input list="status" name="org" class="form-control" form="{form.status}">
<datalist id="status">
<option value="Pending">Pending</option>
<option value="Submitted">Submitted</option>
</datalist>
</td>
</tr>
<tr></tr>
</table>
<input type="submit" value="Submit" style="background-color: white; color: black;" />
<a href="/list" style="background-color: white; color: black;"><input type="button" value="Cancel" /></a>
</div>
</form>
我現在面臨的問題是這樣的:
**用於補充形式工作完全正常,但編輯的形式田不與初始數據填充,如果我用一個大括號是這樣的:**
<td valign="middle" width="70%"><input type="text" name="org" class="form-control" form="{form.org}"></td>
如果我使用雙大括號一樣採取這些初始數據獲取顯示:
<td valign="middle" width="70%"><input type="text" name="org" class="form-control" form="{{form.org}}"></td>
但是在結束雙括號後顯示在表單字段後面。