首页 > 解决方案 > 当我尝试在模式中提交表单时收到错误的请求错误

问题描述

我试图让用户将他们的数字计算提交到一个文本文件,这样我就可以在将它们提交到数据库之前手动检查它们;因此,我在输入 type="submit" 的模式中放置了一个简单的表单,但我不断收到 400 bad request 错误,我无法弄清楚为什么这不起作用。

我正在使用带有烧瓶的 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>

你可以在这里看到现场直播。

我希望你能帮忙。

标签: python-3.xformsmodal-dialog

解决方案


缺少name="action"提交按钮输入字段并且没有处理时事通讯复选框的空值的情况导致错误请求。

因为行动是未定的,你永远无法到达

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

我在开发者模式( F12 )下使用 Chrome来查找 post formdata。当我注意到帖子数据中没有动作和时事通讯时,我实时编辑了 HTML,并且能够成功发布帖子。

祝你继续学习 B


推荐阅读