【发布时间】:2011-09-23 10:54:25
【问题描述】:
我正在编辑表单,当我点击保存时它会正确加载数据,它会在数据库中创建新条目。
这里是视图函数
def create_account(request):
if request.method == 'POST': # If the form has been submitted...
form = AccountForm(request.POST, request.FILES) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
form.save()
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = AccountForm() # An unbound form
return render_to_response('account_form.html', {
'form': form,
})
--
def edit_account(request, acc_id):
f = Account.objects.get(pk=acc_id)
if request.method == 'POST': # If the form has been submitted...
form = AccountForm(request.POST, request.FILES) # A form bound to the POST data
if form.is_valid(): # All validation rules pass
form.save()
return HttpResponseRedirect('/thanks/') # Redirect after POST
else:
form = AccountForm(instance=f) # An unbound form
return render_to_response('account_form.html', {
'form': form,
})
我真的需要单独的编辑功能和单独的删除功能吗?我可以在一个功能中完成所有操作吗
模板
<form action="/account/" method="post" enctype="multipart/form-data" >
{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<p><input type="submit" value="Send message" /></p>
</form>
【问题讨论】:
-
你的意思是我应该使用差异功能而不是保存??
-
请忽略 S.Lott 的评论,它不准确。