斗士狗 2016-08-04 04:14 采纳率: 0%
浏览 47

python flask bucketlist应用程序

I am attempting to build a python flask web app with a tutorial and I am having trouble implementing my signUp method. Tutorial: http://code.tutsplus.com/tutorials/creating-a-web-app-from-scratch-using-python-flask-and-mysql--cms-22972

I get a 500 error when I hit the 'sign up' button: _name = Request.form['inputName'] TypeError: 'cached_property' object has no attribute 'getitem'

Not sure why I am receiving 500 error. Any help would be appreciated. Thanks

Below is my python code:

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)

Here is the javascript ajax code:

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

Here is 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>
  • 写回答

1条回答 默认 最新

  • 关注

    I also encountered the same issue when I was implementing the same recently.

    Change your signUp code in app.py as follows including the import.

    from flask import Flask, render_template, json, request
    
    def signUp():
        _name = request.form['inputName']
        _email = request.form['inputEmail']
        _password = request.form['inputPassword']
    

    Note: it should be request not Request


    This error occurs because Request is the class Flask uses to represent an incoming request; it is not, however, the request itself. Instead, Flask stores the current request, which is an instance of the Request class, in the request variable.
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况