烧瓶请求对象始终为空(GET和POST)

时间:2018-11-08 21:40:52

标签: python postgresql post flask request

这是代码:

from flask import Flask
from flask import request, redirect, url_for, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:root@localhost/gradelink'
db = SQLAlchemy(app)

class Location(db.Model):
    __tablename__ = 'location'
    id = db.Column('id', db.Integer, primary_key=True)
    location_name = db.Column('location_name', db.String(250), unique=True)

    def __init__(self, location_name):
        self.location_name = location_name

    def __repr__(self):
        return '<Location %r' % self.location_name

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

@app.route('/location', methods=['GET', 'POST'])
def location():
    return render_template('location.html');

@app.route('/post_location', methods=['POST'])
def post_location():
    if request.method == "POST":
        location_n = request.form['location_name']

    return redirect(url_for('index'))

@app.route('/location_list', methods=['GET', 'POST'])
def location_list():
    locationList = Location.query.all()
    return render_template('location_list.html', locationList=locationList);

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

我尝试同时使用GET和POST,但结果仍然相同:

对象没有属性“数据”

对象没有属性“ form”

location_list工作完美。

看起来请求总是空的

我的提交表格html:

<!DOCTYPE html>
<html>
<head>
    <title>Location Form</title>
    <meta charset="utf-8" />
</head>
<body>
    <form method="post" action="/post_location">
    <label>Location name:</label>
    <input id="location_name" name="location_name" type="text" />
    <input type="submit" />
    </form>
</body>
</html>

我在做什么错了?

编辑1:

我尝试过:

@app.route('/post_location', methods=['POST'])
def post_location():
    if request.method == "POST":
        location_n = request.form['location_name']

    return redirect(url_for('index'))

现在错误消息是:

“功能”对象没有属性“方法”

编辑2:

我没有一个名为request的函数,并且我没有很多其他函数(这是一个新的新项目)

不,我的任何项目文件夹中都没有flask.py个文件。

编辑3:Flask和Python版本

C:\Users\Daniel>flask --version
Flask 1.0.2
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:57:15) [MSC v.1915 64 bit (AMD64)]

追踪

builtins.AttributeError
AttributeError: 'function' object has no attribute 'method'

Traceback (most recent call last)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2309, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2295, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1741, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\_compat.py", line 35, in reraise
raise value
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\Daniel\AppData\Local\Programs\Python\Python37\lib\site-packages\flask\app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\myproject\flask\__init__.py", line 89, in post_location
if request.method == "POST":
AttributeError: 'function' object has no attribute 'method'

2 个答案:

答案 0 :(得分:0)

所以我最终关闭了所有内容,重新启动计算机和python。我收到了另一个错误(某些行的缩进使用空格而不是TAB),但很容易修复,然后一切正常。

答案 1 :(得分:-1)

在您的locationName之前,放入if request.method == POST:,然后在其下方选中所有内容。