无法从 mysql table 的 Flask CRUD 应用程序中检索数据,即使它已无误插入
Cannot retrieve data from my flask CRUD application from mysql table even though its getting inserted without any error
这是 python 片段
@app.route('/oxygenadmin')
@is_logged_in
def oxygenadmin():
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM oxygen")
data =cur.fetchall()
cur.close()
return render_template('oxygenadmin.html', oxygen=data)
@app.route('/insert', methods=['POST'])
@is_logged_in
def insert():
if request.method == 'POST':
name = request.form['name']
contact = request.form['contact']
email = request.form['email']
place = request.form['place']
cur = mysql.connection.cursor()
cur.execute("INSERT INTO oxygen(name, contact, email, place) VALUES (%s, %s, %s, %s)",(name,contact,email,place))
mysql.connection.commit()
flash("Data inserted successfully", 'success')
return redirect (url_for('oxygenadmin'))
return render_template ('oxygenadmin.html')
这是 html 片段
<div class="row">
<div class="col md-12">
<h2> Oxygen Details <button type="button" class="btn btn-success pull-right" data-toggle="modal" data-target="#mymodal">Enter New Data</button> </h2>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Name</th>
<th>Contact No.</th>
<th>Email</th>
<th>Place</th>
<th>Action</th>
</tr>
{% for row in oxygen %}
<tr>
<td>{{row.0}}</td>
<td>{{row.1}}</td>
<td>{{row.2}}</td>
<td>{{row.3}}</td>
<td>{{row.4}}</td>
<td>
<a href="" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#modaledit">Edit</a>
<a href="" class="btn btn-danger btn-xs" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
和...
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Please insert data!</h4>
</div>
<div class="modal-body">
<form action="{{url_for('insert')}}" method="POST">
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" name="name" required='1'>
</div>
<div class="form-group">
<label>Contact No. :</label>
<input type="text" class="form-control" name="contact" required='1'>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label>Place</label>
<input type="text" class="form-control" name="place" required='1'>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Insert Data</button>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
不知何故我无法让它出现在我的页面上
这是它的样子
in the website itself
但是它被插入到实际的数据库中了
Didnt paste the entire picture because there are some personal details
table 名称是 oxygen,我可能认为它在用名称本身做一些事情,我只是无法理解它
正在改变这个...
{% for row in oxygen %}
<tr>
<td>{{row.0}}</td>
<td>{{row.1}}</td>
<td>{{row.2}}</td>
<td>{{row.3}}</td>
<td>{{row.4}}</td>
<td>
<a href="" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#modaledit">Edit</a>
<a href="" class="btn btn-danger btn-xs" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
</td>
</tr>
{% endfor %}
至
{% for row in oxygen %}
<tr>
<td>{{row["id"]}}</td>
<td>{{row["name"]}}</td>
<td>{{row["contact"]}}</td>
<td>{{row["email"]}}</td>
<td>{{row["place"]}}</td>
<td>
<a href="" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#modaledit">Edit</a>
<a href="" class="btn btn-danger btn-xs" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
</td>
</tr>
{% endfor %}
解决问题
这是 python 片段
@app.route('/oxygenadmin')
@is_logged_in
def oxygenadmin():
cur=mysql.connection.cursor()
cur.execute("SELECT * FROM oxygen")
data =cur.fetchall()
cur.close()
return render_template('oxygenadmin.html', oxygen=data)
@app.route('/insert', methods=['POST'])
@is_logged_in
def insert():
if request.method == 'POST':
name = request.form['name']
contact = request.form['contact']
email = request.form['email']
place = request.form['place']
cur = mysql.connection.cursor()
cur.execute("INSERT INTO oxygen(name, contact, email, place) VALUES (%s, %s, %s, %s)",(name,contact,email,place))
mysql.connection.commit()
flash("Data inserted successfully", 'success')
return redirect (url_for('oxygenadmin'))
return render_template ('oxygenadmin.html')
这是 html 片段
<div class="row">
<div class="col md-12">
<h2> Oxygen Details <button type="button" class="btn btn-success pull-right" data-toggle="modal" data-target="#mymodal">Enter New Data</button> </h2>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Name</th>
<th>Contact No.</th>
<th>Email</th>
<th>Place</th>
<th>Action</th>
</tr>
{% for row in oxygen %}
<tr>
<td>{{row.0}}</td>
<td>{{row.1}}</td>
<td>{{row.2}}</td>
<td>{{row.3}}</td>
<td>{{row.4}}</td>
<td>
<a href="" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#modaledit">Edit</a>
<a href="" class="btn btn-danger btn-xs" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
</td>
</tr>
{% endfor %}
</table>
</div>
</div>
和...
<div id="mymodal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Please insert data!</h4>
</div>
<div class="modal-body">
<form action="{{url_for('insert')}}" method="POST">
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" name="name" required='1'>
</div>
<div class="form-group">
<label>Contact No. :</label>
<input type="text" class="form-control" name="contact" required='1'>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email">
</div>
<div class="form-group">
<label>Place</label>
<input type="text" class="form-control" name="place" required='1'>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Insert Data</button>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
不知何故我无法让它出现在我的页面上
这是它的样子 in the website itself
但是它被插入到实际的数据库中了 Didnt paste the entire picture because there are some personal details
table 名称是 oxygen,我可能认为它在用名称本身做一些事情,我只是无法理解它
正在改变这个...
{% for row in oxygen %}
<tr>
<td>{{row.0}}</td>
<td>{{row.1}}</td>
<td>{{row.2}}</td>
<td>{{row.3}}</td>
<td>{{row.4}}</td>
<td>
<a href="" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#modaledit">Edit</a>
<a href="" class="btn btn-danger btn-xs" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
</td>
</tr>
{% endfor %}
至
{% for row in oxygen %}
<tr>
<td>{{row["id"]}}</td>
<td>{{row["name"]}}</td>
<td>{{row["contact"]}}</td>
<td>{{row["email"]}}</td>
<td>{{row["place"]}}</td>
<td>
<a href="" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#modaledit">Edit</a>
<a href="" class="btn btn-danger btn-xs" onclick="return confirm('Are you sure you want to delete?')">Delete</a>
</td>
</tr>
{% endfor %}
解决问题