简体   繁体   English

FLASK-python ValueError:视图函数未返回响应

[英]FLASK-python ValueError: View function did not return a response

I am trying to run an sql function that inserts data into a table. 我正在尝试运行将数据插入表中的sql函数。 I am following the example explained here but whenever i run the script i get the error "ValueError: View function did not return a response" 我按照此处说明的示例进行操作但是每当我运行脚本时,都会出现错误“ ValueError:View函数未返回响应”

My code looks like this: 我的代码如下所示:

from flask import render_template, flash, redirect, request
from app import app
from .forms import LoginForm
from .forms import RegistrationForm
import sqlite3 as sql

@app.route('/')
@app.route('/index')


@app.route('/registration', methods = ['GET','POST'])
def registration():
    form = RegistrationForm()

    if request.method == 'POST':
      try:
         card_id = request.form['card_id']
         pin = request.form['pin']
         account_id = request.form['account_id']


         with sql.connect("testDB.db") as con:
            cur = con.cursor()

            cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?,?)",(card_id,pin,account_id) )

            con.commit()
            msg = "Record successfully added"
      except:
         con.rollback()
         msg = "error in insert operation"

      finally:
         return render_template("index.html",msg = msg)
         con.close()

what can i be possibly be doing wrong? 我可能做错了什么?

What would be returned if request method is GET? 如果请求方法是GET,将返回什么? you are rendering template only on POST request. 您仅在POST请求时才渲染模板。 correct it. 改正它。

@app.route('/registration', methods = ['GET','POST'])
def registration():
    form = RegistrationForm()

    if request.method == 'POST':
      try:
         card_id = request.form['card_id']
         pin = request.form['pin']
         account_id = request.form['account_id']


         with sql.connect("testDB.db") as con:
            cur = con.cursor()

            cur.execute("INSERT INTO atm_card (card_id,pin,account_id) VALUES (?,?,?,?)",(card_id,pin,account_id) )

            con.commit()
            msg = "Record successfully added"
      except:
         con.rollback()
         msg = "error in insert operation"

      finally:
         return render_template("index.html",msg = msg)
         con.close()
    else:
        return render_template('index.html', form=form)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM