初心者で、私はチュートリアルでurl:http://www.tutorialspoint.com/flask/flask_sqlite.htmのフラスコのチュートリアルを試しています。しかしOperationalError:いいえそのようなテーブル:students
、このエラーの後に
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>
しかし、他のエラーrs。
今、私は次のエラーを取得しています:
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>
がresult.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>
フラスコ-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)
あなたは[MCVE] –
あなたが学生のテーブルを作成し、データベースファイルが同じであることを確認してくださいを持っているので、あなたのコードの残りの部分を十分に追加してください。疑わしい場合は、絶対パスを使用してください。 –