【问题标题】:Editing the Form in Django creates new instance在 Django 中编辑表单会创建新实例
【发布时间】: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 的评论,它不准确。

标签: python django forms


【解决方案1】:

你尝试过这样的事情吗?

 # Create a form to edit an existing Object.
     a = Account.objects.get(pk=1)
     f = AccountForm(instance=a)
     f.save()

# Create a form to edit an existing Article, but use
# POST data to populate the form.
    a = Article.objects.get(pk=1)
    f = ArticleForm(request.POST, instance=a)
    f.save()

【讨论】:

    【解决方案2】:

    您在POST 部分缺少instance 参数。

    而不是这个:

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

    你应该使用这个:

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

    将其添加到添加/编辑表单后,您将能够同时添加/编辑。

    如果instance=None 将添加,如果instance 是实际帐户,则会更新。

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

    【讨论】:

    • 对不起哥们,我还是一头雾水。 1)你能提供我应该使用的代码吗,只有一个编辑和添加功能
    • 在创建中我没有任何参数,但在编辑中我需要传递参数,所以我该如何组合。我还需要不同的模板进行编辑,因为然后我需要将表单操作更改为 edit_account
    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-05
    相关资源
    最近更新 更多