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个回答


最近在实现相同功能时也遇到了相同的问题。</ p>

在app.py中更改注册代码,如下所示,包括导入。</ p>

从烧瓶导入Flask的

 ,render_template,json,请求

def signUp():
     _name = request.form ['inputName']
     _email = request.form ['inputEmail']
     _password = request.form ['inputPassword']
</ code> </ pre>

注意:它应该是 request </ code>而不是 Request </ code> </ p>

</ p>



因为 Request </ code>是Flask用来表示传入请求的类,所以会发生此错误。 但是,它不是请求本身。 相反,Flask将当前请求存储为 request </ code>变量,该请求是 Request </ code>类的实例。
     </ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐