Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 烧瓶:更新数据库中的值的按钮_Python_Html_Flask - Fatal编程技术网

Python 烧瓶:更新数据库中的值的按钮

Python 烧瓶:更新数据库中的值的按钮,python,html,flask,Python,Html,Flask,因此,我目前从数据库中提取一个值,并将其显示在屏幕上。我在它旁边有一个HTML按钮,每次单击它,屏幕上的分数都会增加1。目前,当我点击按钮时,什么都没有发生。屏幕上的数字保持不变 Python文件 @app.route('/') def layout(): db = get_db() try: cur1 = db.execute('SELECT score FROM {tn} where name="Tyler"'.format(tn="Score"))

因此,我目前从数据库中提取一个值,并将其显示在屏幕上。我在它旁边有一个HTML按钮,每次单击它,屏幕上的分数都会增加1。目前,当我点击按钮时,什么都没有发生。屏幕上的数字保持不变

Python文件

@app.route('/')
def layout():
    db = get_db()
    try:
        cur1 = db.execute('SELECT score FROM {tn} where name="Tyler"'.format(tn="Score"))
        score1 = cur1.fetchone()
        if request.method == 'POST':
            db.execute('UPDATE {tn} SET score=score+1 WHERE name="Tyler"'.format(tn="Score"))
            db.commit()
    except:
        flash("This person doesn't exist in our database!")
    return render_template('layout.html', score1=score1)
HTML文件

  <form action="{{ url_for('layout') }}" method="post">
    <p align="center">Tyler</p>
    <div align="center">
      <tr>
        <td>{{ score1.score }}</td>
      </tr> &nbsp; &nbsp;
      <button type="button">+1</button>
    </div>
  </form>

泰勒

{{score1.score} +1
感谢您的帮助。多谢各位


编辑:更新视图和HTML。相同的问题。

您的两个视图功能映射到相同的路由;但是一个URL只能由一个视图提供服务


将increaseScore1函数映射到不同的路径,或者将逻辑组合到一个函数中,该函数检查方法是否为POST,如果为POST,则更新分数,否则只显示它。

?(为什么要插入表名,尤其是在硬编码的情况下?)每当我单击按钮时,数字都不会改变。它只是保持在零(表中的默认值)。一旦我有了基本的功能,我计划进行大量的硬编码,这就是为什么你们会看到一些插值。这很有意义,谢谢你们的回答。我更新了我的代码并尝试了一下,但还是遇到了同样的问题。