gpt4 book ai didi

python flask bucketlist应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 10:10:16 24 4
gpt4 key购买 nike

我正在尝试使用教程构建一个 python flask web 应用程序,但我在实现我的 signUp 方法时遇到了问题。教程:http://code.tutsplus.com/tutorials/creating-a-web-app-from-scratch-using-python-flask-and-mysql--cms-22972

当我点击“注册”按钮时出现 500 错误:_name = Request.form['inputName']TypeError: 'cached_property' 对象没有属性 'getitem'

不确定为什么会收到 500 错误。任何帮助,将不胜感激。谢谢

下面是我的python代码:

from flask import Flask, render_template, json, Request

app = Flask(__name__)

@app.route('/main')
def main():
return render_template('index.html')


@app.route('/showSignUp')
def showSignUp():
return render_template('signup.html')


@app.route('/signUp',methods=['POST'])
def signUp():
_name = Request.form['inputName']
_email = Request.form['inputEmail']
_password = Request.form['inputPassword']

if _name and _email and _password:
return json.dumps({'html':'<span>All fields good !!</span>'})
else:
return json.dumps({'html:':'<span>Enter the required fields</span>'})



if __name__ == "__main__":
app.run(debug=True)

这是 javascript ajax 代码:

$(function () {
$('#btnSignUp').click(function () {
$.ajax({
url: '/signUp',
data: $('form').serialize(),
type: 'POST',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});

这里是signup.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>Python Flask Bucket List App</title>

<link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet" />
<link href="../static/signup.css" rel="stylesheet" />
<script src="../static/js/jquery-3.1.0.js"></script>
<script src="../static/js/signUp.js"></script>

</head>

<body>

<div class="container">
<div class="header">
<nav>
<ul class="nav nav-pills pulls-right">
<li role="presentation"><a href="main">Home</a></li>
<li role="presentation"><a href="#">Sign In</a></li>
<li role="presentation" class="active"><a href="#">Sign Up</a></li>
</ul>
</nav>
<h3 class="text-muted">Python Flask App</h3>
</div>

<div class="jumbotron">
<h1>Bucket List App</h1>
<form class="form-signin">
<label for="inputName" class="sr-only">Name</label>
<input type="name" name="inputName" id="inputName" class="form-control" placeholder="Name" required autofocus />
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" name="inputEmail" id="inputEmail" class="form-control" placeholder="Email address" required autofocus />
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="inputPassword" id="inputPassword" class="form-control" placeholder="Password" required />

<button id="btnSignUp" class="btn btn-lg btn-primary btn-block" type="button">Sign up</button>
</form>
</div>

<footer class="footer">
<p>&copy; Company 2016</p>
</footer>

</div>
</body>
</html>

最佳答案

我最近在实现的时候也遇到了同样的问题。

如下更改 app.py 中的注册代码,包括导入。

from flask import Flask, render_template, json, request

def signUp():
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']

注意:应该是request 不是Request


发生此错误是因为 Request 是 Fl​​ask 用来表示传入请求的类;然而,这不是请求本身。相反,Flask 将当前请求( Request 类的一个实例)存储在 request 变量中。

关于python flask bucketlist应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38758389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com