【问题标题】:Flask App: Could not build url for endpoint 'post_to_db'Flask 应用程序:无法为端点“post_to_db”构建 url
【发布时间】:2019-10-08 02:55:10
【问题描述】:

我是制作 Flask 应用程序的新手,所以我可能会遗漏一些明显的东西。我想要做的是从滑块中获取一个值,并通过滑块值增加我的 postgresql 表中的一个值,然后将我的 postgresql 表中的另一个值增加 1。我收到错误:

werkzeug.routing.BuildError: Could not build url for endpoint 'post_to_db'. Did you mean 'static' instead?

这是我的html:

<form method="POST" action="{{ url_for('post_to_db') }}">
    <input class='slider'id="mydata" name="mydata" type="range" min="0" max="10" value="5" step='1'>
    <div id='button-dock'>
        <button type='submit'>"Show Me Another"</button>
    </div>
</form>

这是我的蟒蛇:

class Dataentry(db.Model):
    __tablename__ = "shoe-ratings"

    id = db.Column(db.Integer, primary_key=True)
    total_score = db.Column(db.Integer)
    num_ratings = db.Column(db.Integer)

    def __init__(self, total_score, num_ratings):
        self.total_score = total_score
        self.num_ratings = num_ratings

    def __repr__(self):
        return '<id {}>'.format(self.id)

    def serialize(self):
        return {
            'id': self.id,
            'total_score': self.total_score,
            'num_ratings': self.num_ratings
        }

@app.route("/virgil_io/submit/", methods=["POST"])
def post_to_db():

    score = request.form['mydata']

    shoe = Dataentry.query.first()
    shoe.total_score = shoe.c.total_score + score
    shoe.num_ratings = shoe.c.num_ratings + 1

    try:
        db.session.add(shoe)
        db.session.commit()
    except Exception as e:
        print("\n FAILED entry\n")
        print(e)
        sys.stdout.flush()
    return "Success"

@APP.route('/virgil_io/')
def index():
    return flask.render_template('index.html')

这是我的目录:

我尝试过的事情:

  • 在我的目录中创建一个 /submit 文件夹。
  • 取出methods=["POST"]

有什么想法吗?谢谢!

【问题讨论】:

  • 请同时显示将该 HTML 模板导入浏览器的代码路径。
  • 可能重复。请参考stackoverflow.com/a/44006237/7622397的答案
  • @DaveW.Smith 我已经编辑过了。
  • @JayJodiwal 谢谢。我看过这个,不认为这是同一个问题。

标签: python sql post flask flask-sqlalchemy


【解决方案1】:

一如既往,这是一件愚蠢的小事。我的应用程序被命名为 APP,但我在此方法中使用了 app

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-21
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多