【问题标题】:Redirect problems with django form in a modal在模态中重定向 django 表单的问题
【发布时间】:2020-08-18 20:26:24
【问题描述】:

我正在尝试在模式中实现一个表单,该表单专注于修改帖子中的评论,我这样做的方式一切正常,问题是当我单击提交按钮时,它会将我发送到其中的 html我有模态,在那里我编辑评论。当我尝试删除将我带到表单第二页的操作表单中的 url 时,它会抛出错误“在分配之前引用的局部变量‘表单’”,例如,如果我在表单操作中输入登录的 url将我发送到那里,但评论没有更新或编辑。

我的想法很简单,当我提交表单时,模态框关闭,模态框从一开始就打开的页面,重新加载或者只是出现已经编辑的评论。

如果您需要更多信息,我可以添加。

views.py

@need_analyst_role
def comment_modify(request, comment_id):

    if 'comment_edit' in request.POST:
        form_comment = FormComment(request.POST)
        if form_comment.is_valid():
            comment_text = form_comment.cleaned_data['text']
            comment = ModelRiskTracking.objects.get(id=comment_id)
            comment.comment = comment_text
            print(comment.comment)
            comment.save()

        else:
            messages.error(request, 'Error!', extra_tags="danger")
   
    context = {}
    context['comment'] = ModelRiskTracking.objects.get(id=comment_id)
    
    return render(request, 'analyst_pages/comment_edit.html', context = context)

modal.html

<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <h2 class="modal-title">Editar comentario</h2>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <form action="{% url 'soc_irisk_modify' comment_id=comment.id %}" method="POST">
            {% csrf_token %}
            <textarea type="text" name="text" class="form-control" rows="15">{{ comment.comment|safe }}</textarea>
            <div class="modal-footer">
                <input type="submit" value="Actualizar" name="comment_edit" onsubmit="setFormSubmitting()" class="btn btn-info btn-sm pull-right" />
            </div>
        </form>
    </div>
</div>

我用一个调用 jQuery 函数的按钮打开模式:

<script type="text/javascript">
    function openModal(url){
        $('#commentModal').load(url, function(){
            $(this).modal('show');
        });
    }
</script>


<button type="button" class="btn btn-primary btn-sm pull-right" data-toggle="modal" data-target="#commentModal" onclick="openModal('{% url 'soc_comment_modify' comment_id=comment.id %}')">

【问题讨论】:

    标签: python html jquery django forms


    【解决方案1】:

    保存模型时,需要重定向用户

    @need_analyst_role
    def comment_modify(request, comment_id):
    
        if 'comment_edit' in request.POST:
        form_comment = FormComment(request.POST)
        if form_comment.is_valid():
            comment_text = form_comment.cleaned_data['text']
            comment = ModelRiskTracking.objects.get(id=comment_id)
            comment.comment = comment_text
            print(comment.comment)
            comment.save()
            return redirect("some_url")
    
        else:
            messages.error(request, 'Error!', extra_tags="danger")
            return redirect("some_url")
    
    context = {}
    context['comment'] = ModelRiskTracking.objects.get(id=comment_id)
    
    return render(request, 'analyst_pages/comment_edit.html', context = context)
    

    【讨论】:

    • 它起作用了,我之前曾尝试过使用“返回 HttpResponseRedirect”,我在另一个答案中读到过,但它不起作用,显然它比这更简单,非常感谢您的回答和时间! !
    猜你喜欢
    • 2013-04-17
    • 1970-01-01
    • 2018-07-19
    • 2016-06-12
    • 1970-01-01
    • 2021-05-14
    • 2012-10-07
    • 2010-11-05
    • 2018-02-14
    相关资源
    最近更新 更多