如何 Update/Edit 数据库内容与 Django 中的表单

how to Update/Edit database content with form in django

我正在尝试使用表单更新内容,但是在我的 html 中,当我单击 link 时没有加载,不知道是什么导致了错误

我的views.py

def edit_address(request, id):
    address = Address.objects.filter(id=id)
    form = Addressform(request.POST)
    if form.is_valid():
        form.user = request.user
        form.save()
    else:
        form = Addressform()
    template_name = "address_edit.html"
    context = {
        "form": Addressform,
        "Address":Address.objects.get(id=id),
    }
    return render(request,template_name,context) 

我认为问题出在 views.py 这就是为什么我只添加这个文件告诉我如果你想看任何其他文件

我的html

<div class="card col-10">
        <div class="form-group form-control-sm">
            <h1 class="text-center">Edit Address</h1>
            <a  class='back' style="float: right;" href="{% url 'accounts:home' %}">Back to site</a> <br>
            {% for Address in address %}
            <form method="POST">
            {% csrf_token %}
             <div class="col-5">{{ form.reciever_name|as_crispy_field }}</div>
             <div class="col-5">{{ form.phone_no|as_crispy_field }}</div>
             <div class="col-5">{{ form.alt_phone_no|as_crispy_field }}</div>
             <div class="col-5">{{ form.state|as_crispy_field }}</div>
             <div class="col-5">{{ form.city|as_crispy_field }}</div>
             <div class="col-5">{{ form.pincode|as_crispy_field }}</div>
             <div class="col-5">{{ form.address|as_crispy_field }}</div>
             <div class="col-5">{{ form.locality|as_crispy_field }}</div>
             <div class="col-5">{{ form.eighteen|as_crispy_field }}</div>
            <br>
            <button type="submit" class="btn btn-outline-success">Add Address</button>
        </form>
    {% endfor %}
    </div>

您需要获取要编辑的地址的实例,并将其作为instance=…传递给AddressForm:

from django.shortcuts import get_object_or_404, redirect

def edit_address(request, id):
    address = get_object_or_404(Address, id=id)
    if request.method == 'POST':
        form = Addressform(request.POST<strong>, instance=address</strong>)
        if form.is_valid():
            form<strong>.instance.user = request.user</strong>
            form.save()
            return redirect('<em>name-of-some-view</em>')
    else:
        form = AddressForm(<strong>instance=address</strong>)
    template_name = 'address_edit.html'
    context = {
        'form': form,
        'Address': address,
    }
    return render(request, template_name, context)

Note: In case of a successful POST request, you should make a redirect [Django-doc] to implement the Post/Redirect/Get pattern [wiki]. This avoids that you make the same POST request when the user refreshes the browser.


Note: It might be better to use the @login_required decorator [Django-doc] as a decorator, and thus wrap the function in it like:

from django.contrib.auth.decorators import <b>login_required</b>

<b>@login_required</b>
def edit_address(request, id):
    # …

Note: You should not create a new form in case the form was not valid. A form that is not valid will store the problems with the input in the form.errors dictionary, and if you render the form (automatically), it will show the errors next to the fields where these errors occur.