You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django编辑按钮异常:点击跳转后表单字段为空

问题:点击编辑按钮后表单为空,手动访问正常

我是Django新手,正在尝试其功能。我创建了用于编辑数据库值的按钮,但功能异常:点击notes.html中的编辑按钮后,页面跳转到edit_note.html,但表单字段为空;手动在浏览器输入地址访问则正常。不清楚问题出在哪里。

相关代码

views.py

class EditNoteView(UpdateView):
    model = Notes
    form_class = NotesForm
    template_name = 'notes/edit_notes.html'
    context_object_name = 'note'

    def get_success_url(self):
        return reverse_lazy('edit_note', kwargs={'pk': self.object.pk})

urls.py

urlpatterns = [
    path('', home, name='home'),
    path('notes/', NoteView.as_view(), name='notes'),
    path('<int:pk>/edit', EditNoteView.as_view(), name='edit_note'),
]

notes.html

{% for i in all_notes %}
    <tr>
    <td>{{ i.notes_category }}</td>
    <td>{{ i.title }}</td>
    <td>{{ i.text }}</td>
    <td>{{ i.reminder_data_time }}</td>
    <td>
        <form action="{% url 'edit_note' i.id %}" method="post">
            {% csrf_token %}
            <div class="btn-small-group">
                <button type="submit">Edit</button>
            </div>
        </form>
    </td>
    <td>
        <div class="btn-small-group">
            <button type="submit">Delete</button>
        </div>
    </td>
{% endfor %}

edit_notes.html

<form action="{% url 'edit_note' note.id %}" method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <div class="btn-small-group">
        <button type="submit">Edit</button>
    </div>
</form>

问题原因

你在notes.html里用POST方法提交表单到EditNoteView,但Django的UpdateView中,POST请求是用来处理表单提交、更新数据的逻辑;而加载带有实例初始数据的表单,是GET请求负责的。当你用POST点击编辑按钮时,视图会尝试处理空的POST数据(你的表单里没有传递任何字段值),导致表单没有绑定对应Note实例的数据,所以显示为空。而手动输入地址访问是GET请求,视图会正确加载对应pk的Note实例并填充表单字段。

解决方案

把notes.html中的编辑按钮改成GET请求跳转,或者直接用a标签代替表单,因为编辑页面只需要先通过GET加载数据,提交修改时再用POST即可。

方案1:修改表单method为GET

{% for i in all_notes %}
    <tr>
    <td>{{ i.notes_category }}</td>
    <td>{{ i.title }}</td>
    <td>{{ i.text }}</td>
    <td>{{ i.reminder_data_time }}</td>
    <td>
        <!-- 将method改为get,无需csrf_token -->
        <form action="{% url 'edit_note' i.id %}" method="get">
            <div class="btn-small-group">
                <button type="submit">Edit</button>
            </div>
        </form>
    </td>
    <td>
        <!-- Delete按钮需要POST,保留表单和csrf_token -->
        <form action="{% url 'delete_note' i.id %}" method="post">
            {% csrf_token %}
            <div class="btn-small-group">
                <button type="submit">Delete</button>
            </div>
        </form>
    </td>
{% endfor %}

方案2:用a标签代替表单(更简洁)

{% for i in all_notes %}
    <tr>
    <td>{{ i.notes_category }}</td>
    <td>{{ i.title }}</td>
    <td>{{ i.text }}</td>
    <td>{{ i.reminder_data_time }}</td>
    <td>
        <div class="btn-small-group">
            <a href="{% url 'edit_note' i.id %}" class="button">Edit</a>
        </div>
    </td>
    <td>
        <form action="{% url 'delete_note' i.id %}" method="post">
            {% csrf_token %}
            <div class="btn-small-group">
                <button type="submit">Delete</button>
            </div>
        </form>
    </td>
{% endfor %}

另外补充:建议把urls.py中的编辑路径改成<int:pk>/edit/(加末尾斜杠),符合Django的URL规范,避免不必要的重定向问题。

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:33:54