【发布时间】:2016-08-05 00:41:12
【问题描述】:
作为初学者,我一直在尝试教程点的烧瓶教程,网址为:http://www.tutorialspoint.com/flask/flask_sqlite.htm。
但是,在这个错误之后
jinja2.exceptions.TemplateNotFound TemplateNotFound: home.html
应该显示: 1.添加新记录 2. 显示列表
我做了以下事情:
<h3>Students (<a href = "{{ url_for('new_student') }}">Add new record
</a>)</h3>
<h3> (<a href = "{{ url_for('list') }}">Show List
</a>)</h3>
不过,我认为可能还有其他错误。
现在,我收到以下错误:
sqlite3.OperationalError
OperationalError: no such table: students
任何人都可以帮助制作教程吗?非常感激。 *ps:请不要标记这篇文章。如果您不能或不想提供帮助。把这个问题留给愿意帮助初学者的人吧。谢谢。
参考: http://www.tutorialspoint.com/flask/flask_sqlite.htm
这是@Wayne 要求的复制和粘贴:
‘student.html’:
<html>
<body>
<form action = "{{ url_for('addrec') }}" method = "POST">
<h3>Student Information</h3>
Name<br>
<input type = "text" name = "nm" /></br>
Address<br>
<textarea name = "add" ></textarea><br>
City<br>
<input type = "text" name = "city" /><br>
PINCODE<br>
<input type = "text" name = "pin" /><br>
<input type = "submit" value = "submit" /><br>
</form>
</body>
</html>
结果.html:
<!doctype html>
<html>
<body>
result of addition : {{ msg }}
<h2><a href = "\">go back to home page</a></h2>
</body>
</html>
list.html:
<!doctype html>
<html>
<body>
<table border = 1>
<thead>
<td>Name</td>
<td>Address>/td<
<td>city</td>
<td>Pincode</td>
</thead>
{% for row in rows %}
<tr>
<td>{{row["name"]}}</td>
<td>{{row["addr"]}}</td>
<td> {{ row["city"]}}</td>
<td>{{row['pin']}}</td>
</tr>
{% endfor %}
</table>
<a href = "/">Go back to home page</a>
</body>
</html>
Flask-SQLite 应用程序:
from flask import Flask, render_template, request
import sqlite3 as sql
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/enternew')
def new_student():
return render_template('student.html')
@app.route('/addrec',methods = ['POST', 'GET'])
def addrec():
if request.method == 'POST':
try:
nm = request.form['nm']
addr = request.form['add']
city = request.form['city']
pin = request.form['pin']
with sql.connect("database.db") as con:
cur = con.cursor()
cur.execute("INSERT INTO students (name,addr,city,pin)
VALUES (?,?,?,?)",(nm,addr,city,pin) )
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
return render_template("result.html",msg = msg)
con.close()
@app.route('/list')
def list():
con = sql.connect("database.db")
con.row_factory = sql.Row
cur = con.cursor()
cur.execute("select * from students")
rows = cur.fetchall();
return render_template("list.html",rows = rows)
if __name__ == '__main__':
app.run(debug = True)
【问题讨论】:
-
请添加足够的其余代码,以便您拥有minimal reproducible example
-
确保您创建了学生表并且数据库文件相同。如有疑问,请使用绝对路径。
标签: python html sqlite templates flask