连接sqlite3数据库与Python - 用户输入

时间:2018-01-10 03:17:50

标签: python flask sqlite

我有一个使用FLASK和Python运行的网页,并且连接了一个数据库sqlite3。我创建了一个名为guestbook的页面,我要求用户输入一些关于它们的详细信息:Name,Email,Age,以及将该信息发送到服务器和数据库。我正在页面上检索它以显示用户的输入。

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

当我运行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>

1 个答案:

答案 0 :(得分:0)

frameinput.pack(side=LEFT, fill=BOTH, expand=YES) 中的method = post更改为<form action。完整的行应该是:

method='POST'