模态表单未提交Django

时间:2019-09-13 22:42:04

标签: django forms bootstrap-modal

我有一个不提交的模式。我正在使用Django基本模型。我认为CSRF令牌可能有问题,因为它一直在抛出该错误。我什至没有解决“出现问题”的错误消息。我添加了一些内容来捕获form.errors,但是它没有显示任何错误。传递了变量,但是由于某些原因-它不起作用。

def voting(request):
    if request.method == 'POST':
        user=request.user
        if user.is_authenicated:
            price, created = Voting.objects.get_or_create(
                             user=request.user,
                             anonymous_user=False,
                             object_id = Object.objects.get(
            objectid=request.POST.get('objectid')
            ),
                             thumbs_up=request.POST.get('thumbs_up'),
                           thumbs_down=request.POST.get('thumbs_down'),
                             comments=request.POST.get('comments')
                              )
            price.save()
        else:
            pass
        response_data = 'success'
        return HttpResponse(json.dumps(response_data), content_type="application/json")
   else:
       return HttpResponse(json.dumps({"message": "Something went wrong"}),content_type="application/json")

这是html代码:

<div class="voting text-right"><small>See a problem?</small>
 <a onclick="thumbsup('{{ data.object.objectid }}'>
   <i class="fas fa-thumbs-up"></i>
 </a>
 <a data-toggle="modal" data-target="#feedback" data-object-id="{{ data.object.objectid }}">
 <i class="fas fa-thumbs-down"></i>

它不起作用,也不提交。我也不断遇到CRSF令牌问题。

这是模态:

<!-- Modal -->
<div class="modal fade" id="feedback" tabindex="-1" role="dialog" aria-labelledby="feedback" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Feedback</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 id="feedback-form" action="/api/voting/" method="post">
                    {% csrf_token %}
                    <input type="hidden" name="objectid">
                    <input type="hidden" name="thumbs_up" value="False">
                    <input type="hidden" name="thumbs_down" value="True">

                    <p>We would love to hear your thoughts!</p>
                    <div class="form-group">
                        <label for="comments">Please leave your feedback below:</label>
                        <textarea class="form-control" id="comments" name="comments" rows="3" required></textarea>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-secondary">Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

0 个答案:

没有答案