【问题标题】:html does not send post to viewhtml 不发送帖子以查看
【发布时间】:2020-06-16 16:18:23
【问题描述】:

我有一个提交表单,例如:

<div class="form-group">
    <div class="checkbox">
        <label for="id_active_state">
            <div class="icheckbox_square-red checked" style="position: relative;">
                <input type="checkbox" name="active_state" class="" id="id_active_state" checked=""><ins class="iCheck-helper"></ins>
            </div> active</label>
    </div>
</div>
<div class="form-group">

    <div class="button_holder">
        <button type="submit" name="submit" class="btn btn-primary" value="send">
            submit
        </button>
    </div>

</div>

而且应该在视图中处理:

def special_offer_edit_view(http_request):
    action_verbose_name = 'the_view'
    special_offer_id = http_request.GET.get('id')
    special_offer_obj = SpecialOffer.objects.filter(id=special_offer_id)
    for data in special_offer_obj:
        sp_obj = {'active_state': data.active_state}

    if http_request.method == 'POST':
        print('hi')

    return render(...)

它引发 503 错误,并且不发送“POST”方法和数据。它的问题是什么?

【问题讨论】:

  • 您在输入字段和提交按钮周围缺少&lt;form action="whereYourActionTakesPlace" method="POST"&gt;。您的字段不知道 a) 他们应该使用 post (method) 和 b) 它应该指向 (action) 的位置。

标签: python html django post django-views


【解决方案1】:

您的输入应通过元素名称active_state 访问,否则您已给出

像这样None

special_offer_id = http_request.GET.get('id')

您的 render(...) 应该返回带有现有模板的 HttpResonse

render(http_request,"template.html",{"context" : 2})

在设置中设置DEBUG=True以获取错误日志

csrf_token嵌入html

<form method="POST">
<!-- Your html here -->
{% csrf_token %}
</form>

【讨论】:

  • render 就像你写的一样,我在 HTML 文件的开头也使用了&lt;form method="POST"&gt;
  • 您收到错误日志了吗?否则很难提供帮助
  • 我没有使用这条线{% csrf_token %},它是正确的。 Tnx
  • 你可以找到更多here
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 2018-10-08
  • 2019-12-16
相关资源
最近更新 更多