0
我是新的python瓶,並希望擺脫404未找到錯誤。我想插入一個sqllite表並列出插入的項目。請幫忙。由於404未找到。使用sqllite輸入數據使用sqllite
views.py
from flask import Flask, render_template, request, url_for, redirect
import sqlite3 as sql
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
# route for handling the login page logic
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
error = 'Invalid Credentials. Please try again.'
else:
return redirect(url_for('index'))
return render_template('login.html', error=error)
@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("databsae.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("databsae.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()
students.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>
list.html
<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>
result.html
<!doctype html>
<html>
<body>
result of addition : {{ msg }}
<h2><a href = "/">go back to home page</a></h2>
</body>
</html>
爲什麼在list.html裏有td的奇怪格式? 地址>/td < – toha
它沒有修復404找不到的錯誤。 –
你什麼時候得到404?渲染模板時會發生嗎?訪問特定端點時會發生嗎? – dirn