在Django中编辑表单创建新的实例 [英] Editing the Form in Django creates new instance

查看:91
本文介绍了在Django中编辑表单创建新的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在编辑表单,加载数据正确地购买,当我点击保存它创建新的数据库条目。 b
$ b

  def create_account(request):


如果request.method =='POST':#如果表单已提交...
form = AccountForm(request.POST,request.FILES)#一个绑定到POST数据的表单
如果form.is_valid():#所有验证规则通过
form.save()
return HttpResponseRedirect('/ thanks /')#POST之后重定向
else:
form = AccountForm()#未绑定的形式

返回render_to_response('account_form.html',{
'form':form,
})

-

  def edit_account(request,acc_id):

f = Account。 object.get(pk = acc_id)
如果request.method =='POST':#如果表单已经提交...
form = AccountForm(request.POST,request.FILES)#绑定到POST数据的表单
如果form.is_valid():#所有验证规则都通过
form.save()
return HttpResponseRedirect( '/ thanks /')#POST之后重定向
else:
form = AccountForm(instance = f)#一个未绑定的形式

返回render_to_response('account_form.html',{
'form':form,
})

我真的需要具有单独的编辑功能,分开删除。我可以在一个功能中执行所有操作



模板

 < form action =/ account /method =postenctype =multipart / form-data> 
{%csrf_token%}
{%表单%}中的字段
< div class =fieldWrapper>
{{field.errors}}
{{field.label_tag}}:{{field}}
< / div>
{%endfor%}
< p>< input type =submitvalue =发送消息/>< / p>
< / form>


解决方案

你缺少 参数 POST 部分。



而不是这样:

  form = AccountForm(request.POST,request.FILES)#绑定到POST数据的表单

您应该使用这个:

  form = AccountForm (request.POST,request.FILES,instance = f)#绑定到POST数据的表单

一旦你添加到添加/编辑表单,您将能够同时添加/编辑。



如果 instance =没有,如果实例是一个实际帐户,则更新

  def edit_account(request,acc_id = None):
如果acc_id:
f = Account.objects.get(pk = acc_id)
else:
f =无

如果request.method =='POST':#如果表单已经提交...
form = AccountForm(reque st.POST,request.FILES,instance = f)#绑定到POST数据的表单
如果form.is_valid():#所有验证规则通过
form.save()
返回HttpResponseRedirect('/ thanks /')#POST之后重定向
else:
form = AccountForm(instance = f)#一个未绑定的形式

返回render_to_response('account_form.html' ,{
'form':form,
})


I am editing the form , it loads data correctly buy when i hit save it creates new entry in database.

Here is the view functions

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,
    })

Do i really need to have separate function of editing and separate for deleting. Can i do all in one function

template

    <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>

解决方案

You are missing the instance argument at the POST section.

Instead of this:

form = AccountForm(request.POST, request.FILES) # A form bound to the POST data

You should use this:

form = AccountForm(request.POST, request.FILES, instance=f) # A form bound to the POST data

Once you add that to the add/edit form you will be able to add/edit at the same time.

It will add if instance=None and update if instance is an actual account.

def edit_account(request, acc_id=None):
    if acc_id:
        f = Account.objects.get(pk=acc_id)
    else:
        f = None

    if request.method == 'POST': # If the form has been submitted...
        form = AccountForm(request.POST, request.FILES, instance=f) # 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,
    })

这篇关于在Django中编辑表单创建新的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆