#views
import pymysql
def get_date(request):
conn = pymysql.connect(
host='localhost',
port=3306,
user='root',
passwd='mysql',
db='homework2',
charset='utf8',
autocommit=True
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
# sql = "select * from ?"
# sql = sql.replace('%s','?')
sql = "select * from teacher_info"
# value = "teacher_info"
# cursor.execute(sql,value)
cursor.execute(sql)
user_list = cursor.fetchall()
with open(r'templates/get_data.html','r',encoding='utf8') as fr:
data = fr.read()
return render(request,'get_data.html',{'user_list':user_list})
#html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>get_data</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<link href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://use.fontawesome.com/aaee4b5d24.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h2 class="text-center">数据展示</h2>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
<th>salary</th>
<th>job</th>
</tr>
</thead>
<tbody>
{% for user in user_list %}
<tr>
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.age }}</td>
<td>{{ user.salary }}</td>
<td>{{ user.job }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>