提问者:小点点

Python Django编辑HTML行并更新数据库


我有一个表,我希望添加一个编辑按钮,以可视方式和在数据库中更新特定的记录。

我有的HTML。

{% for work_entry in work_entries %}
                {% if work_entry.date == date.date %}
                <form action="{% url 'work_entries:object_edit' work_entry.id %}" method="post">
                    {% csrf_token %}
                <tr>
                    <td>
                            <button onclick="return confirm('Are you sure you want this edit?')">Edit
                            </button>
                            </td>
                    <td> <form action="{% url 'work_entries:object_delete' work_entry.id %}" method="post">
                            {% csrf_token %}
                            <button onclick="return confirm('Are you sure you want to delete this record?')">Delete
                            </button>
                            </form>
                            </td>
                    <td>{{ work_entry.id }}</td>
                    <td><input type="text" value="{{ work_entry.description }}" name="description"></td>
                    <td><input type="number" value="{{ work_entry.num_hours }}" name="hours"></td>
                </tr>
                </form>
                {% endif %}
            {% endfor %}

views.py

def object_edit(request, object_id):

    record = get_object_or_404(WorkEntry, pk=object_id)

    if request.method == "POST":
        record.description = request.POST["description"]
        record.num_hours = request.POST["hours"]
        record.save()
        return redirect("/employeePage")

    return render(request, "employeePage.html")

和urls.py。

app_name = "work_entries"

urlpatterns = [
    path("", views.employee_view, name="employeePage"),
    url(r"^delete/(?P<object_id>[0-9]+)/$", views.object_delete, name="object_delete"),
    url(r"^edit/(?P<object_id>[0-9]+)/$", views.object_edit, name="object_edit"),
]

我在as中使用了一个input标记,我认为这将允许我更改数据并保存它。 但是,这会在/employeepage/edit/14/'description'错误处给出MultiValueDictKeyError。 我对jquery没有太多的经验,我从研究中看到它可以工作,但我似乎没有得到正确的。 有人可以帮助或甚至建议,我应该如何处理这将是有用的。

注意:已经有一个按钮来删除记录,我尝试了一个类似的方法来编辑,但它不工作。


共1个答案

匿名用户

我完全鼓励你使用Django提供的表格,它会让你的生活更轻松。

我完全鼓励你不要使用一个表单来删除你的东西,它应该是一个简单的链接,它将避免在表单中有一个表单。 我觉得你的问题在这里。 如果表单中的按钮位于中间,浏览器就不可能知道要提交表单的哪些部分。

同样,您有两个按钮,但它们都不是submit类型。

如果你不想用Django表单,一种方法就是


{% for work_entry in work_entries %}
    {% if work_entry.date == date.date %}
        <form action="{% url 'work_entries:object_edit' work_entry.id %}" method="post">
        {% csrf_token %}
                <tr>
                    <td>
                        <button>
                            <a href="{% url 'work_entries:object_delete' work_entry.id %}" onclick="return confirm('Are you sure you want this edit?')">Delete</a>
                        </button>
                    </td>
                    <td>                                    <button type="submit"           onclick="return confirm('Are you sure you want to update this record?')">
                            Update
                        </button>
                    </td>
                    <td>{{ work_entry.id }}</td>
                    <td><input type="text" value="{{ work_entry.description }}" name="description"></td>
                    <td><input type="number" value="{{ work_entry.num_hours }}" name="hours"></td>
                </tr>
        </form>
    {% endif %}
{% endfor %}

这不是最美的方法我试着保留你的建筑

相关问题