这是代码:
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 但仍然是相同的结果:
对象没有属性“数据”
对象没有属性“形式”
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任何项目文件夹中都没有任何文件。
Helenr
30秒到达战场
相关分类