【发布时间】:2020-09-03 19:02:57
【问题描述】:
我正在创建一个 Flask 网络应用程序。我遇到了一个小问题,将从一页捕获的数据连接到下一页。我可以将通过通知路由从“address.html”捕获的地址的数据输入到表 1(“位置”)中。但我似乎无法从“notice.html”中提取数据,它捕获提交类型按钮值并应该将其与外键一起输入到表 2(“notice_types”)中。下面是 SQL 表,再往下是路由和 html 页面。
class Address (db.Model):
__tablename__="locations"
id = db.Column(db.Integer, primary_key=True)
address = db.Column(db.String, nullable=False)
notice_types = db.relationship("Notice", backref="locations", lazy=True)
def add_notice(self, notice):
n = Notice(notice=notice, address_id=self.id)
db.session.add(n)
db.session.commit()
class Notice (db.Model):
__tablename__="notice_types"
id = db.Column(db.Integer, primary_key=True)
notice = db.Column(db.String, nullable=False)
address_id = db.Column(db.Integer, db.ForeignKey("locations.id"), nullable=False
路线:
@app.route('/')
def location():
return render_template('address.html')
@app.route('/notice', methods = ["GET", "POST"])
def notice():
location = Address.query.all()
address = request.form.get('address')
if not address:
return render_template("error.html")
location = Address(address=address)
db.session.add(location)
db.session.commit()
return render_template('notice.html', address=address, location=location)
@app.route('/action', methods = ["GET", "POST"])
def action():
notice = request.form.get('first_n')
try:
address_id = request.args.get("address", None)
except ValueError:
return render_template("error.html")
address = Address.query.get(address_id)
if not address:
return render_template("error.html")
notice_type = Notice(notice=notice, address=address)
db.session.add(location)
db.session.commit()
return render_template('thirtysixty.html', address=address)
以下是两个 html 页面的示例:
<form action="{{ url_for('notice') }}" method="post">
<div class="form-group">
<label align='left' class='thick'>Address: </label>
<br>
<input list="locations" name="address" id="address" type="text" placeholder="street,
city, state, zip code">
</div>
<p class='a'> 123 Example Southeast, Oakland, CA, 94590</p>
<div class="form-group">
<button type="submit" class="btn btn-primary" minlength="6">Submit</button>
<form action="{{ url_for('action') }}" method="post">
<div class="row">
<div class="form-group">
<input type="submit" class='button' name="first_n" value="3/6 Notice" id="go">
</div>
</form>
【问题讨论】:
标签: python html flask sqlalchemy