当我尝试在模式中提交表单时,出现错误的请求错误应该怎么解决?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (101)

试图让用户把他们的gematria计算提交给一个文本文件,这样我就可以在将它们提交到数据库之前手动检查它们; 因此我已经在输入type=“submit”的模态中放置了一个简单的表单,但我不断收到400错误的请求错误,为什么这种方式不起作用。

我正在使用Python3.6服务器端,并且不使用html引导程序。

服务器端:

if request.form["action"] == "Send":
    Name = request.form["name"]
    Email = request.form["mail"]
    GematriaNum = request.form["gematria"]
    Source = request.form["source"]
    Calc = request.form["calc"]
    LIT = request.form["transliteration"]
    LANGY = request.form["translation"]
    NEWSL = request.form["newsletter"]
    BODYOF = Name + Email + GematriaNum + Source + Calc + LIT + LANGY + NEWSL

    with open("submiss.txt", "w") as submiss:
        submiss.write(BODYOF)

    return render_template("index.html")
    return redirect(url_for('index'))

网页代码:

    <!-- ...it's part of a nav bar. -->
    <!-- Trigger/Open The Modal -->
    <li><a id="myBtn">Submit your Calculations to the Database!</a></li>
</ul>

<!-- The Modal -->
<div id="myModal" class="modal">

<!-- Modal content -->
<div class="modal-content">
    <span class="close">&times;</span>
    <form action="." method="POST"><span>
        Name:<br>
        <textarea style="resize:none" name="name" cols="25" rows="1" maxlength="25"></textarea><br>
        E-mail:<br>
        <textarea style="resize:none" name="mail" cols="25" rows="1" maxlength="25"></textarea><br>
        Gematria Number:<br>
        <textarea style="resize:none" name="gematria" cols="25" rows="1" maxlength="25"></textarea><br>
        Source Text [i.e. Exodus 2:12]:<br>
        <textarea style="resize:none" name="source" cols="25" rows="1" maxlength="25"></textarea><br>
        Calculation in Hebrew, Greek or English:<br>
        <textarea style="resize:none" name="calc" cols="50" rows="1" maxlength="100"></textarea><br>
        Transliteration (if applicable):<br>
        <textarea style="resize:none" name="transliteration" cols="50" rows="1" maxlength="100"></textarea><br>
        Meaning and Usage:<br>
        <textarea style="resize:none" name="translation" cols="50" rows="1" maxlength="100"></textarea><br>
        <br>
        <input type="checkbox" checked="checked" name="newsletter" style="margin-bottom:15px"> Sign up for our newsletter?<br>
        <input type="submit" value="Send"></span>
        <input type="reset" value="Reset">
    </form>
    <br>
    Thanks!
</div>

</div>

<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}
</script>
提问于
用户回答回答于

name="action"在提交按钮输入字段丢失,没有处理时事通讯复选框的空值,导致错误的请求。

由于动作未设置,不会执行

if request.form["action"] == "Send":

在开发者模式(F12)中使用Chrome 来查找帖子表单数据。当我注意到行动和时事通讯不在后数据中时,我编辑了HTML。

扫码关注云+社区

领取腾讯云代金券