【问题标题】:Flask request object always empty (GET and POST)Flask 请求对象始终为空(GET 和 POST)
【发布时间】:2019-04-12 11:33:53
【问题描述】:

这是代码:

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'))

现在错误信息是:

'function'对象没有属性'method'

编辑 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'

【问题讨论】:

  • 您是否在某处定义了一个名为request 的函数?您的路径中是否有一个名为flask.py 的文件?您如何初始化和运行 Flask 应用程序?做。你知道如何在调试模式下运行你的代码吗?这样做并检查 request 实际上是什么,因为听起来它实际上不是来自 Flask 的 request 代理。
  • 或许发布您的完整代码会有所帮助。
  • 我在问题中添加了完整代码,如何检查请求?代码已经在调试中
  • 我承认我看不出代码有什么问题。你如何运行它?错误是如何报告的?你有追溯吗? Flask 服务器是否启动?
  • 哦,你的 HTML 中要写小写的 method="post"。

标签: python postgresql post flask request


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在您的 locationName 之前,输入 if request.method == POST: 然后在下面的所有内容上按标签。

    【讨论】:

    • 我尝试了你的建议,但没有奏效(看看我的编辑1)
    猜你喜欢
    • 1970-01-01
    • 2017-05-12
    • 2017-05-22
    • 1970-01-01
    • 2015-09-08
    • 2016-06-01
    • 1970-01-01
    • 2019-03-06
    • 2018-01-06
    相关资源
    最近更新 更多