使用表单更改现有模型数据

时间:2016-01-07 14:55:32

标签: python django django-forms

我正在尝试实现一个视图,该视图显示用于捕获数据的表单和包含用户捕获数据的表。该表有一个表单,每行有两个按钮,使用POST提交“更改”或“删除”以及给定表行中对象的对象ID。

我的Django视图如下所示:

def captureData(request):
    form = MyForm(request.POST or None)

    if request.method == "POST":
        if 'delete' in request.POST:
            # User hits "Delete" button in displayed objects table.
            try:
                del_object = MyObject.objects.filter(user = request.user).get(id = request.POST['delete'])
                del_object.delete()
            except:
                # Do something ... 
            return redirect('captureData')
        elif 'change' in request.POST:
            # User hits "Change" button in displayed objects table.
            ch_object = MyObject.objects.filter(user = request.user).get(id = request.POST['change'])
            form = MyForm(instance = ch_object)
            if form.is_valid():
                form.save()
                return redirect('captureData')
        else:
            # New data to be added to the database.
            if form.is_valid():
                new_object = form.save(commit = False)
                new_object.user = request.user
                new_object.save()
            return redirect('captureData')

    objects = Object.objects.filter(user = request.user)
    context = {'form': form, 'objects': objects}
    return render(request, 'myTemplate.html', context)

这就是myTemplate.html的样子:

{%  extends "base.html" %}

{% block content %}
        <h3>Data capturing</h3>
        <p>
            <!-- First form, responsible for capturing data -->
            <form method="POST" action=""> {% csrf_token %}
                {{form}}
                <input type ='submit' value='Save'/>
            </form>
        </p>
        <h3>Captured data</h3>
            <table>
                <thead>
                    <tr>
                        <th>#</th>
                        <th>Name</th>
                        <th>Address</th>
                        <th>City</th>
                        <th colspan="2">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for object in objects %}
                    <tr>
                    <td>{{forloop.counter}}</td>
                    <td>{{object.name}}</td>
                    <td>{{object.address}}</td>
                    <td>{{object.city}}</td>
                    <!-- Second form (per row), responsible for submitting a "delete" or "change" -->
                    <form action="" method="POST">{% csrf_token %}
                        <td>
                        <button type="submit" value="{{object.id}}" name="change" id="object{{object.id}}">Change</button>
                        </td>
                        <td>
                        <button type="submit" value="{{object.id}}" name="delete" id="object{{object.id}}">Delete</button>
                        </td>
                        </form>
                    </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
{% endblock %}

“删除”部分工作正常。问题是“改变”部分。它使用选定的对象数据成功填充表单,但是当我从提交按钮点击捕获时,如果数据已经存在,则会遇到导致新数据行或错误的else:子句。原因很明显:新的POST数据不再包含“更改”标记。

如何将elif:部分与else:部分分开?

0 个答案:

没有答案