【问题标题】:how to make submisison boxes work with sql/sqlite and flask,python?如何使提交框与 sql/sqlite 和烧瓶、python 一起使用?
【发布时间】:2020-11-15 07:40:34
【问题描述】:

好的,所以我正在为自己制作一个测试库存系统,在其中手动提交每个项目,但我在提交时遇到了问题。我已经解决了所有的 html 和 python,但是当尝试提交所有的东西时,会弹出一个错误,说 IntegrityError: NOT NULL constraint failed: posts.condition.

这是我的代码:

应用程序.py

from flask_cors import CORS
from models import create_post, get_posts

app = Flask(__name__)

CORS(app)

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

    if request.method == 'GET':
        pass 

    if request.method == 'POST':
        post = request.form.get('post')
        item = request.form.get('item')
        TimenDate = request.form.get('TimenDate')
        condition = request.form.get('condition')
        history = request.form.get('history')

        create_post(item,TimenDate, condition, history)


    posts = get_posts()
    
   
    

    return render_template('index.html', posts=posts)

if __name__ == '__main__':
    app.run(debug=True)

ABB.sql 文件:

    create table posts (
        id integer primary key autoincrement,
        item text not null,
        TimenDate text not null,
        condition text not null,
        history text not null
);

models.py 文件:

from os import path

ROOT = path.dirname(path.relpath((__file__)))

def create_post(item,TimenDate,condition, history):
    con = sql.connect(path.join(ROOT, 'database.db'))
    cur = con.cursor()
    cur.execute('insert into posts (item, TimenDate,condition, history) values( ?, ?, ?, ?)', (item, TimenDate, condition, history))
    con.commit()
    con.close()
 
def get_posts():
    con = sql.connect(path.join(ROOT, 'database.db'))
    cur = con.cursor()
    cur.execute('select * from posts')
    posts = cur.fetchall()
    return posts

我的 html 文件:

<!doctype html>
<html>
    <body>
        <form action='/' role='form' method='POST'>
            <input placeholder='Item' name='item'>
            <input placeholder='TimenDate' name='TimenDate'>
            <input placeholder='Condition' name='Condition'>
            <input placeholder='History' name='History'>
            <input type='submit' value='Submit'>
        </form>
        {% for post in posts %}
            <div>{{ item[1] + ': ' + TimenDate[3]+ ': ' +Condition[5]+ ': ' +History[6]}}</div>
        {% endfor %}
    </body>
</html>

最后是错误:

create_post(item,TimenDate, condition, history)

File "/Users/mylaptop/ABB/models.py", line 9, in create_post
Open an interactive python shell in this framecur.execute('insert into posts (item, TimenDate,condition, history) values( ?, ?, ?, ?)', (item, TimenDate, condition, history))

感谢你们的任何反馈。我确定它与 abb.sql 文件和数据库有关,但我不知道它是什么以及为什么。

【问题讨论】:

    标签: python sql sql-server flask mysql-python


    【解决方案1】:

    您在表单发布的内容和路由期望的内容之间存在几个大小写冲突。例如

    <input placeholder='Condition' name='Condition'>
    

    condition = request.form.get('condition')
    

    【讨论】:

    • 我试过了,但又出现了另一个错误。它说“项目未定义”。我认为这与这段代码有关{% for post in posts %} &lt;div&gt;{{ item[1] + ': ' + TimenDate[2]+ ': ' +condition[3]+ ': ' +history[4]}}&lt;/div&gt; {% endfor %}
    • 您现在遇到了不同类别的错误。尝试post.item 而不是item[1]。其他数据项也是如此。
    • UndefinedError: 'tuple object' 没有属性 'item'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2013-01-13
    • 2022-01-09
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    相关资源
    最近更新 更多