简体   繁体   English

如何将数据从 javascript 发送到 python Flask 后端?

[英]How do I send data from javascript to python flask back-end?

I know this question has been answered already but I do not understand.我知道这个问题已经回答了,但我不明白。 I am new to flask and co-developing a chat website.我是 Flask 和共同开发聊天网站的新手。 This includes a database for users to log in这包括一个供用户登录的数据库

My signup.html has a considerable size, so I will only include the part I'm having troubles with:我的 signup.html 有相当大的尺寸,所以我只会包括我遇到问题的部分:

{% extends "base.html" %}
{% block title %} Sign up {% endblock %}
{% block content %}
<form method="POST" id="signupform" action="#" onsubmit="checkForm()">
    <div class="form-group row">    
        <div class="col-sm-10">
            <div class="form-check">
                <input class="form-check-input" type="checkbox" id="gridCheck1">
                <label class="form-check-label" for="gridCheck1">
                    I have read and agreed to the <a href="/TOS">Terms Of Service</a>
                </label>
                <small id="checkSmall" class="form-text text-muted"></small>
            </div>
        </div>
    </div>
</form>
{% endblock %}

My javascript:我的javascript:

function checkForm() {
    var isChecked = document.getElementById("gridCheck1").checked
    if (isChecked == false) {
        console.warn("TOS not agreed to")
        document.getElementById("checkSmall").innerHTML = "Please agree to the TOS before continuing.";
    } else {
        console.log("Tos were agreed to")
    }
}

My python (again, not the whole file):我的python(再次,不是整个文件):

@app.route("/signup", methods=["GET", "POST"])
def signup():
    if request.method == "POST":
        Name = request.form["Name"]
        Email = request.form["Email"]
        Password = request.form["Password"]
        usr = users(Name, Email, Password)
        acc = [Name, Email, Password]
        for value in acc:
            if len(value) == 0 or " "*len(value) == value:
                flash(f"{value} cant be blank")
                return redirect(url_for("signup"))
                break
            elif len(value) > 100:
                flash(f"{value} cant be more then 100 chars")
                return redirect(url_for("signup"))
                break
        db.session.add(usr)
        db.session.commit()
        flash("Signed up.")
        return redirect(url_for('home'))
    return render_template("signup.html")

I dont want to redirect to the home page unless the user actually agrees.除非用户真正同意,否则我不想重定向到主页。 I already have the javascript set up to pop a little message if they dont check that checkbox, and I need to tell the server to stop from the js.如果他们不选中该复选框,我已经将 javascript 设置为弹出一条小消息,并且我需要告诉服务器停止 js。 Could anyone help?有人可以帮忙吗? PS: I could also use another way to acheive my goal. PS:我也可以用另一种方式来实现我的目标。 Also, just in case this can help anyone solve my problem, I'm using bootstrap.另外,以防万一这可以帮助任何人解决我的问题,我正在使用引导程序。 I'm not good at html, so anything helps.我不擅长 html,所以任何事情都有帮助。

我能想到的最简单的方法(我认为这会起作用;如果有误,请纠正我):

<input class="form-check-input" type="checkbox" id="gridCheck1" required>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM