【问题标题】:FLASK form returning data as NonetypeFLASK 表单将数据返回为 Nonetype
【发布时间】:2021-09-13 10:56:44
【问题描述】:

HTML

在这段代码中,我试图在烧瓶中检索表单数据,但它返回非类型而不是字符串。我也在表单中包含了 name 属性,但它仍然给出了 Nonetype。实际上我想访问表单数据以打印在 csv 或 txt 文件中。也请帮忙。

<!doctype html>
<html lan="en" dir="ltr">

<head>
    <meta charset="UTF-8">
    <title>Details</title>
    <link rel="stylesheet" href="styles.css">
    <meta name="viewport" content="width=device-width" ,initial-scale=1>
</head>
<style>
    @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200&display=swap');
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}
body{
    display: flex;
    height: 100vh;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg,#71b7e6,#9b59b6);
}

.container{
    max-width: 700px;
    width: 100%;
    background: #fff;
    padding: 25px 30px;
    border-radius: 20px;

}
.container .title{
    font-size: 25px;
    font-weight: 500;
    position: relative;
}
.container .title::before{

    content: "";
    position: absolute;
    left: 0;
    bottom: 0;
    height: 3px;
    width: 50px;
    background: linear-gradient(135deg,#71b7e6,#9b59b6);
}
.container form .user-detail{
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}
form .user-detail .input-box{
    margin: 1px 0 5px 0;
    width: cal(100% /2-20px);
}

.user-detail .input-box .details{
    font-weight: 500;
    margin-bottom: 5px;
}
.user-detail .input-box input{
    height: 45px;
    width: 100%;
    outline: none;
    border-radius: 5px;
    border: 1px solid #ccc;
    padding-left: 15px;
    font-size: 16px;
    border-bottom-width: 2px    ;
    transition: all 0.3s ease;
}
.user-detail .input-box input:focus,
.user-detail .input-box input:valid{
    border-color:#9b59b6;
}
form .gender-details{
    font-size: 20px;
    font-weight: 500;
}
form .gender-details .gender-title{
    font-size: 15px;
    font-weight: 500;
}
form .gender-details .category{
        display: flex;
        width: 80%;
        justify-content: space-between;
        margin: 14px 0;
}
.gender-details .category label{
    display: flex;
    align-items: center;
}
.gender-details .category .dot{
    height: 18px;
    width: 18px;
    background: #d9d9d9;
    border-radius: 50%;
    margin-right: 10px;
    border: 2px solid transparent;

}
#dot-1:checked ~ .category .one,
#dot-2:checked ~ .category .two,
#dot-3:checked ~ .category .three{
    border-color: #d9d9d9;
    background: #9b59b6;
}

form input[type="radio"]{
    display: none;
}
form .button{
    height: 45px;
    margin: 45px 0;
}
form .button input{
    height: 100%;
    width: 100%;
    outline: none;
    color: #fff;
    font-size: large;
    font-weight:1000;
    border-radius: 20px;
    background: linear-gradient(135deg,#9b59b6,#71b7e6);
}
form .button input:hover{
    background: linear-gradient(-135deg,#9b59b6,#71b7e6);
}
</style>
<body>
    <div class="container">
        <div class="title">Personal Details</div>
        <br>
        <form action ="form_2">
            <div class="user-detail">
                <div class="input-box">
                    <span class="details">Full Name</span>
                    <input type="text" name="full_name" class="form-control" placeholder="Enter your Name">
                </div>
                <br>
                <div class="input-box">
                    <span class="details">Username</span>
                    <input name="username" placeholder="Enter your Username">
                </div>
                <br>
                <div class="input-box">
                    <span class="details">Contact </span>
                    <input name="contact"  placeholder="Enter your contact number">
                </div>
                <br>
                <div class="input-box">
                    <span class="details">Address</span>
                    <input name="address" placeholder="Enter your Address">
                </div>
                <br>
            </div>
            <div class="gender-details">
                <input type="radio" name="Gender" id="dot-1">
                <input type="radio" name="Gender" id="dot-2">
                <input type="radio" name="Gender" id="dot-3">
                <span class="gender-title">Gender</span>
                    <div class="category">
                        <label for="dot-1">
                            <span class="dot one"></span>
                            <span class="gender">Male</span>
                        </label>
                        <label for="dot-2">
                            <span class="dot two"></span>
                            <span class="gender">Female</span>
                        </label>
                        <label for="dot-3">
                            <span class="dot three"></span>
                            <span class="gender">Prefer not to Say</span>
                        </label>
                    </div>
            </div>
            <div class="button">
                <input type="submit" value="Register">
            </div>
            
        </form>
    </div>
</body>

</html>

烧瓶

from flask import Flask, render_template,request
app = Flask(__name__)



@app.route('/')
def cover():
    return render_template('cover.html')
    # return "Hello World"

@app.route('/signup/form_2',methods=['GET','POST','DELETE'])
def phpu():
    name=request.form.get("full_name")
    username=request.form.get("username")
    contact=request.form.get("contact")
    address=request.form.get("address")  
    print(type(name))
render_template('form_2.html',title=title,name=name,username=username,contact=contact,address=address)
    if __name__ == "__main__":
    app.run(debug=True, port=8000)

请帮助这一直返回无。 谢谢!!

【问题讨论】:

  • 表格在哪里?
  • 动作必须是
  • 使用 if request.method == "POST" ,然后访问 post vars
  • @charchit 我使用了你的方法,只是注意到它没有采用 POST 方法。请就如何将方法设为“POST”提出建议。
  • 只需将其添加到表单标签中的表单method="post",&lt;form action ="form_2" method="post"&gt;

标签: python php html flask


【解决方案1】:

表单操作不正确。必须如下:

<form action ="signup/form_2">

如果更新不起作用,请为您的 POST 方法添加 if 语句,如下所示:

@app.route('/signup/form_2',methods=['GET','POST','DELETE'])
def phpu():
    if request.method=='POST':
        name=request.form.get("full_name")
        ...etc

【讨论】:

  • 谢谢,但它将页面重定向到相同的 url,我也尝试更改它,但它仍然是一样的。输出是 NoneType 本身。如果还有其他问题,请提出建议。
猜你喜欢
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多