【问题标题】:Connecting data from one route to another in Flask and inputting the data into the SQL table with SQL-alchemy在 Flask 中将数据从一个路由连接到另一个路由,并使用 SQL-alchemy 将数据输入到 SQL 表中
【发布时间】: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


    【解决方案1】:

    解决了我自己的问题:

    SQL 表应保持原样。相反,我需要在通知路由中执行正确的数据库查询,并在突出显示的模板中正确使用 jina2。以下是固定代码;它再次讨论了如何通过使用模板和 jinja2 将表单数据从一条路线正确移动到另一条路线。使用这些行,我能够将数据连同正确的键一起输入到两个 SQL 表中。

    @app.route('/')
    def location():
        return render_template('address.html')
    
    @app.route("/notice", methods = ["GET", "POST"])
    def notice():
    
        address = request.form.get("address")
        if not address:
           return render_template("error.html")
    
        db.execute("INSERT INTO locations (address) VALUES (:address)",
               {"address": address})
        db.commit()
        addresses = db.execute("SELECT * FROM locations).fetchall()
        return render_template("notice.html", addresses=addresses) 
    
    
      @app.route('/action', methods = ["GET", "POST"])
          def action():
    
        notice = request.form.get('first_n')
        try: 
            address_id = request.form.get("address_id")
        except ValueError: 
            return render_template("error.html")
    
       db.execute("INSERT INTO notice_types (notice, address_id) VALUES 
       (:notice, :address_id)", {"notice":notice, "address_id":address_id})
       db.commit()
       return render_template("action.html") 
    

    固定的 HTML: 问题中突出显示的第一个模板很好。下面是需要修复的模板。

    <form action="{{ url_for('action') }}" method="post">
      <div class="form-group">
          {% for address in addresses %}
             <input type="hidden" name="address_id" value="{{address.id}}">
          {% endblock %}
          <div class="form-group">
            <input type="submit" class='button' name="first_n" value="3/6 
               Notice" data-value="3/6 notice">
          </div>
        </form>
    

    【讨论】:

      猜你喜欢
      • 2019-09-22
      • 2017-08-16
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 2018-01-06
      相关资源
      最近更新 更多