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

Django社交帖子编辑后无法保存问题排查求助

社交帖子编辑保存异常问题

需求目标

用户可编辑自己发布的社交帖子,流程为点击编辑、修改内容后保存。

问题现象

编辑社交帖子后无法完成保存,但关闭编辑弹窗后页面会临时保留修改内容,无报错信息,终端显示POST请求返回200状态码。

现有功能

  • 可发布社交帖子到列表,列表仅显示帖子前200字符;
  • 点击详情按钮可查看完整帖子内容;
  • 编辑按钮仅对帖子发布者可见,点击后弹出带原有帖子内容的编辑弹窗。

已尝试方案

  • 编写了分开的social_post_detail和social_post_edit视图函数;
  • 尝试将功能合并为单个social_post_detail视图函数(两种写法);
  • 在视图中添加messages模块捕获表单错误,但未得到有效信息;
  • 怀疑JS/CSS导入问题,但未定位到具体原因。

相关代码

视图函数(部分示例)

@login_required
def social_post_detail(request, pk):
    social_post = get_object_or_404(social_post, pk=pk)
    form = None
    if request.user == social_post.created_by:
        if request.method == 'POST':
            print(request.POST)
            form = social_postForm(request.POST, instance=social_post)
            if form.is_valid():
                form.save()
                return redirect('social_post_list')
        else:
            form = social_postForm(instance=social_post)
    return render(request, 'social_post_detail.html', {'social_post': social_post, 'form': form})

HTML代码(模态框部分)

<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">Edit social_post</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form method="post">
                    {% csrf_token %}
                    <div class="form-group">
                        <label for="title">social_post Title</label>
                        <input type="text" class="form-control" name="title" value="{{ social_post.title }}">
                    </div>
                    <div class="form-group">
                        <label for="description">social_post Description</label>
                        <textarea class="form-control" name="description">{{ social_post.description }}</textarea>
                    </div>
                    {{ form.as_p }}
                    <button type="submit" class="btn btn-primary">Save Changes</button>
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
                </form>
            </div>
        </div>
    </div>
</div>

终端日志示例

[02/Feb/2023 00:44:13] "POST /social_post/social_post/1/ HTTP/1.1" 200 7142

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 08:23:55