使用Python连接sqlite3 DB-用户输入
我有一个使用FLASK和Python运行的网页,并连接了一个数据库sqlite3。我创建了一个名为guestbook的页面,要求用户输入有关他们的一些详细信息:姓名、电子邮件、年龄,并将这些信息发送到服务器和数据库。我在页面上检索它以显示用户的输入 这里的整个代码几乎都是从讲座中复制/粘贴的,因为讲师向我们展示了代码以及它是如何一块一块地工作的,然后由我们将其粘合在一起。所以当我比较代码时,它们看起来是一样的,但是我的代码给了我一个错误,我只是看不到错误。我没有任何打字错误,文件有正确的名称,行与我的讲师完全相同。如果你能指出我的错误,我将不胜感激 当我运行python文件时,它运行正常,或者至少我认为可以打印运行python文件的屏幕 无论页面有多大程度上没有加载,令人困惑的是,在命令窗口中,我无法看到错误,因为我激活了调试器,它通常会写入错误。所以我迷路了错误在哪里,因为我不知道该去哪里寻找 内部服务器错误 Python代码:使用Python连接sqlite3 DB-用户输入,python,flask,sqlite,Python,Flask,Sqlite,我有一个使用FLASK和Python运行的网页,并连接了一个数据库sqlite3。我创建了一个名为guestbook的页面,要求用户输入有关他们的一些详细信息:姓名、电子邮件、年龄,并将这些信息发送到服务器和数据库。我在页面上检索它以显示用户的输入 这里的整个代码几乎都是从讲座中复制/粘贴的,因为讲师向我们展示了代码以及它是如何一块一块地工作的,然后由我们将其粘合在一起。所以当我比较代码时,它们看起来是一样的,但是我的代码给了我一个错误,我只是看不到错误。我没有任何打字错误,文件有正确的名称,行
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>
布拉格-留言簿
输入名称:
输入电子邮件:
输入年龄:
客人姓名
来宾电子邮件
客人年龄
{entries%%中的条目的百分比}
{{entry[0]}
{{entry[0]}
{{entry[0]}
{%endfor%}
更改
方法=在OMG中发布,就是这样!!!!!非常感谢。它起作用了!在看了几个小时该死的代码之后!对烟火
<form action ="{{url_for('guestbook')}}" method = 'POST'>