【问题标题】:HTML form not submitting using python flaskHTML表单未使用python烧瓶提交
【发布时间】:2020-12-23 13:55:45
【问题描述】:

你知道为什么这个 HTML 表单不提交吗? 我只是没有在 Flask 中收到“POST”消息,因此无法从那里执行我想要的。

HTML 部分(带有“取消隐藏”测试的按钮)

            <button id="take_test" class="btn btn-info" onclick="Showtest()">Take the test</button>

            <form style="visibility:hidden" id="addiction2" action:"/test" method:"post">

                <div class="form-group">
                    <fieldset id:"group1">
                    <p>Over the last 12 months, did you experience an impaired control over gaming (frequency, intensity, duration, termination, context)?</p>
                    <input type="radio" id="1" name="group1" value="1" required>
                    <label for="1">Yes</label><br>
                    <input type="radio" id="0" name="group1" value="0" required>
                    <label for="0">No</label><br>
                    </fieldset>
                </div>

                <div class="form-group">
                    <fieldset id:"group2">
                    <p>Over the last 12 months, did you experience an increasing priority given to gaming to the extent that gaming takes precendence over other life interests and daily activities?:</p>
                    <input type="radio" id="1" name="group2" value="1" required>
                    <label for="1">Yes</label><br>
                    <input type="radio" id="0" name="group2" value="0" required>
                    <label for="0">No</label><br>
                    </fieldset>
                </div>

                <div class="form-group">
                    <fieldset id:"group3">
                    <p>Over the last 12 months, did you experience a continuation or escalation of gaming despite the occurance of negative consequences (i.e. significant impairment in personal, family, social, educational, occupational or other important areas of functioning)?</p>
                    <input type="radio" id="1" name="group3" value="1" required>
                    <label for="1">Yes</label><br>
                    <input type="radio" id="0" name="group3" value="0" required>
                    <label for="0">No</label><br>
                    </fieldset>
                </div>

                <div class="form-group">
                    <fieldset id:"group4">
                    <p>Over the last 12 months, did you try to stop gaming or reduce the time spent gaming, but failed to do that?</p>
                    <input type="radio" id="1" name="group4" value="1" required>
                    <label for="1">Yes</label><br>
                    <input type="radio" id="0" name="group4" value="0" required>
                    <label for="0">No</label><br>
                    </fieldset>
                </div>
                <div class="form-group">
                    <fieldset id:"group5">
                    <p>Over the last 12 months, did you ever lie to your spouse, family or friends about the amount of time you have spent gaming?</p>
                    <input type="radio" id="1" name="group5" value="1" required>
                    <label for="1">Yes</label><br>
                    <input type="radio" id="0" name="group5" value="0" required>
                    <label for="0">No</label><br>
                    </fieldset>
                </div>
                <br>
                <button class="btn btn-secondary" type="submit">Submit</button>
                </form>
            </main>

            <script>
            function Showtest() {
                var x = document.getElementById("addiction2");
                x.style.visibility = "visible";
                var y = document.getElementById("take_test");
                y.style.display = "none";
                var z = document.getElementById("footer");
                z.style.display = "block";
            }
            </script>

我尝试使用 post 方法将变量添加到分数中的 Python 部分(我知道这可能可以以更智能的方式完成,但现在甚至没有收到 POST(如日志所示)并且重定向不会发生)

@app.route("/test", methods=["GET", "POST"])
@login_required
def test():
    if request.method == "POST":

        #get all the answers, store them in variables as 1 or 0
        q1 = request.form['group1']
        q2 = request.form['group2']
        q3 = request.form['group3']
        q4 = request.form['group4']
        q5 = request.form['group5']

        score = q1+q2+q3+q4+q5

        print(score)

        #mark database as test completed for this user

        return redirect("/results")

【问题讨论】:

  • actionmethod 属性的 form 元素使用冒号而不是等号。会不会是这个问题?

标签: python html flask post


【解决方案1】:

这里是如何在烧瓶中做一个简单的表格。您需要安装 Flask-WTF 包。
包含所有可用字段的文档链接:link

from flask_wtf import FlaskForm
from wtforms import PasswordField, SubmitField
from wtforms.validators import DataRequired, Length

class LoginForm(FlaskForm):
    # create password entry box
    password = PasswordField('Password', validators=[DataRequired(), Length(4, 32)])
    # create submit button
    submit = SubmitField("Login")

@app.route("/login", methods=["GET", "POST"])
def auth_user():
    form = LoginForm() # load form
    if form.validate_on_submit():
        # do something if form submitted
        if form.password.data == "your password":
           print("logged in!")
    else:
        # else sent form
        return flask.render_template("login.html", form=form)

以及html相关页面

<html>
    <body>
        <div>
            <form method="POST" action="">
                {{ form.hidden_tag() }}
                <fieldset>
                    <legend>Log in</legend>
                    <div>
                        {{ form.password.label }}
                        {{ form.password }}
                    </div>
                </fieldset>
                <div>
                    {{ form.submit }}
                </div>
            </form>
        </div>
    </body>
</html>

【讨论】:

  • 但是flask_wtf对于表单提交是强制性的吗?我有其他页面(登录、注销、更改密码等)在没有烧瓶 WTF 的情况下工作得很好,只有这个页面失败了。我似乎无法理解为什么。例如,这个帖子做得非常好: <...> {% block main %}
    <...>
  • 不,当然不是,但我发现它真的很有用,因为我不是网络开发人员
【解决方案2】:

找到了!一个错字(使用 : 而不是 =) - 就是这样。

我有什么:

<form style="visibility:hidden" id="addiction2" action:"/test" method:"post">

我需要的东西:

<form style="visibility:hidden" id="addiction2" action="/test" method=:"post">

感谢大家的帮助!

【讨论】:

    【解决方案3】:

    尝试使用 url_for 处理程序添加 POST URL。

     <form style="visibility:hidden" id="addiction2" action="{{ url_for('test') }}" method="post">
    
                    <div class="form-group">
                        <fieldset id:"group1">
                        <p>Over the last 12 months, did you experience an impaired control over gaming (frequency, intensity, duration, termination, context)?</p>
                        <input type="radio" id="1" name="group1" value="1" required>
                        <label for="1">Yes</label><br>
                        <input type="radio" id="0" name="group1" value="0" required>
                        <label for="0">No</label><br>
                        </fieldset>
                    </div>
    
                    <div class="form-group">
                        <fieldset id:"group2">
                        <p>Over the last 12 months, did you experience an increasing priority given to gaming to the extent that gaming takes precendence over other life interests and daily activities?:</p>
                        <input type="radio" id="1" name="group2" value="1" required>
                        <label for="1">Yes</label><br>
                        <input type="radio" id="0" name="group2" value="0" required>
                        <label for="0">No</label><br>
                        </fieldset>
                    </div>
    
                    <div class="form-group">
                        <fieldset id:"group3">
                        <p>Over the last 12 months, did you experience a continuation or escalation of gaming despite the occurance of negative consequences (i.e. significant impairment in personal, family, social, educational, occupational or other important areas of functioning)?</p>
                        <input type="radio" id="1" name="group3" value="1" required>
                        <label for="1">Yes</label><br>
                        <input type="radio" id="0" name="group3" value="0" required>
                        <label for="0">No</label><br>
                        </fieldset>
                    </div>
    
                    <div class="form-group">
                        <fieldset id:"group4">
                        <p>Over the last 12 months, did you try to stop gaming or reduce the time spent gaming, but failed to do that?</p>
                        <input type="radio" id="1" name="group4" value="1" required>
                        <label for="1">Yes</label><br>
                        <input type="radio" id="0" name="group4" value="0" required>
                        <label for="0">No</label><br>
                        </fieldset>
                    </div>
                    <div class="form-group">
                        <fieldset id:"group5">
                        <p>Over the last 12 months, did you ever lie to your spouse, family or friends about the amount of time you have spent gaming?</p>
                        <input type="radio" id="1" name="group5" value="1" required>
                        <label for="1">Yes</label><br>
                        <input type="radio" id="0" name="group5" value="0" required>
                        <label for="0">No</label><br>
                        </fieldset>
                    </div>
                    <br>
                    <button class="btn btn-secondary" type="submit">Submit</button>
                    </form>
                </main>
    
                <script>
                function Showtest() {
                    var x = document.getElementById("addiction2");
                    x.style.visibility = "visible";
                    var y = document.getElementById("take_test");
                    y.style.display = "none";
                    var z = document.getElementById("footer");
                    z.style.display = "block";
                }
                </script>
    

    【讨论】:

      猜你喜欢
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 2017-03-25
      • 1970-01-01
      • 2013-01-13
      • 2018-07-26
      相关资源
      最近更新 更多