【问题标题】:Connecting sqlite3 DB with Python - User input使用 Python 连接 sqlite3 DB - 用户输入
【发布时间】:2018-01-10 03:17:49
【问题描述】:

我有一个使用 FLASK 和 Python 运行的网页,并连接了一个数据库 sqlite3。我创建了一个名为留言簿的页面,要求用户输入有关他们的一些详细信息:姓名、电子邮件、年龄,并将该信息发送到服务器和数据库。我在页面上检索它以显示用户的输入。

这里的整个代码几乎是从讲座中复制/粘贴的,因为讲师向我们展示了代码以及它是如何工作的,然后由我们将它们粘合在一起。所以当我比较代码时,它们看起来是一样的,但是我的却给了我一个错误,我只是没有看到错误。我没有任何错别字,文件名称正确,行与我的讲师完全相同。如果您能指出我的错误,我将不胜感激。

当我运行 python 文件时,它运行正常,或者至少我认为是运行 python 文件的打印屏幕

页面无法加载,令人困惑的是,在我激活调试器后,在命令窗口中我看不到错误,它通常会写入错误。所以我迷失了错误在哪里,因为我不知道在哪里看。

内部服务器错误

Python 代码:

from datetime import date
from flask import Flask, render_template, url_for, redirect, request
import sqlite3

app = Flask(__name__)
DB_FILE = 'mydb'

@app.route('/')
def main():
    return render_template('PragueMainPage.html')
#----------------------------------------------------------

@app.route('/history')
def history():
    return render_template('History.html')
#----------------------------------------------------------

@app.route('/gallery')
def gallery():
    return render_template('Gallery.html')
#----------------------------------------------------------

@app.route('/guestbook', methods=['POST'])
def guestbook():
    _insert(request.form['Name'], request.form['Email'], request.form['Age'])
    return redirect(url_for('view'))

def _insert(name, email, age):
    params = {'Name':name, 'Email':email, 'Age':age}
    connection = sqlite3.connect(DB_FILE)
    cursor = connection.cursor()  
    cursor.execute("insert into guestbook VALUES (:name, :email, :age)",params)
    connection.commit()
    cursor.close()

@app.route ('/guestbook', methods=['POST','GET'])
def view():
    connection = sqlite3.connect(DB_FILE)
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM guestbook")
    rv = cursor.fetchall()
    cursor.close()
    return render_template("Guestbook.html",entries=rv)


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

还有 HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" type="text/css" href= "{{ url_for('static', filename='stylesheets/ingridpython.css') }}"/>
        <title> Prague - Guestbook</title>
    </head>
<body>
<div class ="flex-container">
   <div class ="navigation">
        <ul>
            <li><a href ='/'>Main Page </a></li>
            <li><a href ='history'>History </a></li>
            <li><a href ='gallery'>Gallery </a></li>
            <li><a href ='guestbook'>Guestbook </a></li>
       </ul>
   </div>   
</div>

<div id ="guestContainer">
    <form action ="{{url_for('guestbook')}}" method = post>
        Enter name:   <input type ="text"   name ="Name">
        Enter E-mail: <input type ="email"  name ="Email">
        Enter Age:    <input type ="text"   name ="Age">
        <input class ="submit" type ="submit" value ="Submit">
    </form>
 </div>

<div id ="guestContainerAnswer">
    <table>
        <tr>
            <th width ="350"> Guest name</th>
            <th width ="600"> Guest E-mail</th>
            <th width ="100"> Guest Age</th>
        </tr>
        {% for entry in entries %}
        <tr>
            <th>{{ entry[0]}}</th>
            <th>{{ entry[0]}}</th>
            <th>{{ entry[0]}}</th>
        </tr>
        {% endfor %} 
    </table>
</div>

【问题讨论】:

    标签: python flask sqlite


    【解决方案1】:

    &lt;form action 中的method = post 更改为method='POST'。完整的行应该是:

    <form action ="{{url_for('guestbook')}}" method = 'POST'>
    

    【讨论】:

    • 天哪,原来是这样!!!!!!谢谢你!有用!经过数小时查看该死的代码!是的!!!! 烟花
    猜你喜欢
    • 1970-01-01
    • 2017-11-28
    • 2011-07-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2018-09-14
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多