简体   繁体   English

我正在添加一个新行,而不是对我的数据库中的现有行进行更改

[英]I am adding a new row instead of make changes in a existing row in my DB

I am trying to save my form in my data base.我正在尝试将我的表单保存在我的数据库中。 But my code adds a new row instead of save changes to the existing one.但是我的代码添加了一个新行而不是保存对现有行的更改。 where is my mistake?我的错误在哪里?

view.py查看.py

    def settings(request):
        error = ''
        if request.method == 'POST':
            new_form = TrafficSourcesForm(request.POST)
            if new_form.is_valid():
                new_form.save()
    
    
    
            else:
                error = 'Something went wrong!'
new_form = TrafficSourcesForm()
    forms = [TrafficSourcesForm(instance=x) for x in TrafficSources.objects.all()]

   
    return render(request, 'mainpage/dashboard.html', {'new_form': new_form, 'forms': forms, 'error': error})

template模板

<div class="table table-striped table-hover">

            <div class="table-row">
                <th style="width: 42%">Name</th>
                <th style="width: 43%">Token</th>
                <th style="width: 15%">Action</th>
            </div>
        {% for form in forms %}
            <div class="table-row">
                <form method="POST">
                {% csrf_token %}
                <div class="table-cell">{{ form.name }}</div>
                <div class="table-cell">{{ form.token }}</div>
                <div class="table-cell"><button class="btn btn-lg btn-success w-100"">Save</button></div>
                </form>
            </div>
        </div>

If its not clear: I am showing all the table from my databese on the page.如果不清楚:我在页面上显示了我的数据库中的所有表格。 I want to edit them and save again to the database.我想编辑它们并再次保存到数据库中。

Because you are using POST data and form to create a new instance on every request:因为您正在使用 POST 数据和表单在每个请求上创建一个新实例:

...
if request.method == 'POST':
    new_form = TrafficSourcesForm(request.POST)
    if new_form.is_valid():
        new_form.save()
...

To edit an object, you first need to retrieve the instance, which is normally done using its unique identifier (pk) .要编辑 object,您首先需要检索实例,这通常使用其unique identifier (pk)来完成。 Although normally you would send this ID using theurl dispatcher captured value.虽然通常您会使用url 调度程序捕获的值发送此 ID。 I am using a hidden field in this case:在这种情况下,我使用了一个隐藏字段:

mainpage/dashboard.html主页/dashboard.html

<body>
    {% if forms %}
        <table class="table table-striped table-hover">
            <thead>
                <tr>
                    <th style="width: 42%">Name</th>
                    <th style="width: 43%">Token</th>
                    <th style="width: 15%">Action</th>
                </tr>
            </thead>

            <tbody>
                {% for form in forms %}
                <form method="POST">
                    {% csrf_token %}
                    <tr>
                        <td>{{ form.name }}</td>
                        <td>{{ form.token }}</td>
                        <input type="hidden" value="{{ form.instance.pk }}" name="id">
                        <td class="table-cell"><button class="btn btn-lg btn-success w-100">Save</button></td>
                    </tr>
                </form>
                {% endfor %}
            </tbody>
        </table>
    {% endif %}

    <form method="POST">
        {% csrf_token %}
        {{new_form.as_p}}
        <div class="table-cell"><button class="btn btn-lg btn-success w-100">Create</button></div>
    </form>
</body>

views.py视图.py

def settings(request):
    error = ''
    if request.method == 'POST':
        new_form = TrafficSourceForm(request.POST)
        pk = request.POST.get('id')
        
        if new_form.is_valid():
            if pk:
                TrafficSource.objects.filter(id=pk).update(**new_form.cleaned_data)
            else:
                TrafficSource.objects.create(**new_form.cleaned_data)
                
        else:
            error = 'Something went wrong!'
    new_form = TrafficSourceForm()
    forms = [TrafficSourceForm(instance=x) for x in TrafficSource.objects.all()]
 
    return render(request, 'mainpage/dashboard.html', {'new_form': new_form, 'forms': forms, 'error': error})

Id recommend getting the specific object you want to modify.我建议获取您要修改的特定 object。 Ex.前任。 traffic_source = TrafficSources.objects.get(id=<id_here>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM