当我尝试在模态中提交表单时,我收到了错误的请求错误

时间:2018-06-06 16:34:29

标签: python-3.x forms modal-dialog

我正在尝试允许用户将gematria计算提交到文本文件,以便我可以在将它们提交到数据库之前手动检查它们;因此我在一个模态中放置了一个简单的形式,输入类型=“提交”,但我一直得到一个400错误的请求错误,我无法弄清楚为什么这不起作用。

我正在使用带有烧瓶的Python3.6服务器端,而我没有使用带有html的bootstrap。

服务器端:

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>

您可以看到网站直播here

我希望你能提供帮助。

1 个答案:

答案 0 :(得分:3)

在提交按钮输入字段中缺少name="action",并且没有处理新闻稿空值的处理情况复选框导致错误请求。

因为行动未设置,所以你永远不会进入

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

我在开发者模式( F12 )中使用Chrome来查找帖子形式数据。当我发现动作和简报不在帖子数据中时,我编辑了HTML live并且能够成功发布帖子。

Goodluck继续学习B