前端代码与数据库的拼接
步骤
- 1、拿到前端的代码
- 2、拿到数据库的数据(用到数据库的查询,和数据的获取)
- 3、进行拼接,将数据库的内容替换到前端(使用正则)
- 4、返回拼接的内容
第一步


第二步

第三步和第四步


这些是框架之外的工作,代码如下:
import re
from pymysql import connect
url_dict = dict()
def route(url):
def set_fun(func):
def call_fun(*args, **kwargs):
print("添加权限")
return func(*args, **kwargs)
print("装饰后的地址:", call_fun)
print("装饰后的请求地址:", url)
url_dict[url] = call_fun
return call_fun
return set_fun
def application(file_path):
response_line = "HTTP/1.1 200 OK\r\n"
response_head = "content-type:text/html;charset=utf-8\r\n"
try:
method = url_dict[file_path]
response_body = method()
except Exception as e:
response_line = "HTTP/1.1 404 NOT FOUND\r\n"
response_body = "not page is show!"
return response_line, response_head, response_body
@route("/index.html")
def index():
with open("./templates/index.html", 'r') as f:
content = f.read()
row_str = """
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<input type="button" value="添加" id="toAdd" name="toAdd" systemidvaule="000007">
</td>
</tr>
"""
conn = connect(host='localhost', port=3306, database='stock_db', user='root', password='mysql', charset='utf8')
cs1 = conn.cursor()
cs1.execute("select * from info;")
data = cs1.fetchall()
cs1.close()
conn.close()
table_str = ""
for temp in data:
table_str += row_str % (temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7])
content_new = re.sub("\{%content%\}", table_str, content)
return content_new
@route("/center.html")
def center():
with open("./templates/center.html", 'r') as f:
content = f.read()
conn = connect(host='localhost', port=3306, database='stock_db', user='root', password='mysql', charset='utf8')
cs1 = conn.cursor()
cs1.execute(
"select info.code,info.short,info.chg,info.turnover,info.price,info.highs,focus.note_info from info inner join focus on focus.info_id = info.id;")
data = cs1.fetchall()
cs1.close()
conn.close()
row_str = """
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<a type="button" class="btn btn-default btn-xs" href="/update/300268.html"> <span class="glyphicon glyphicon-star" aria-hidden="true"></span> 修改 </a>
</td>
<td>
<input type="button" value="删除" id="toDel" name="toDel" systemidvaule="300268">
</td>
</tr>
"""
table_str = ""
for temp in data:
table_str += row_str % (temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6])
content_new = re.sub('\{%content%\}', table_str, content)
return content_new