[英]Flask Render template is not displaying the message parsed to html page?
Problem: 问题:
Iam having a flask based search bar on search iam fetching the data from the MySQL and want to display on the same page. Iam在搜索IAM上有一个基于烧瓶的搜索栏,该IAM从MySQL获取数据并希望显示在同一页面上。
Code: 码:
@app.route('/search', methods=['GET', 'POST'])
def search():
try:
if request.method == "POST":
conn = MySQLdb.connect(user="analyst", passwd="abc123", db="somedb", host="127.0.0.1")
cursor =conn.cursor()
cursor.execute("SELECT * from some where domain = '%s'"%(request.form['search']))
logger.info('Query is {}'.format("SELECT * from samples where md5 = '%s'"%(request.form['search'])))
data = cursor.fetchall()
logger.info('Data fetched is {}'.format(str(data)))
return render_template("search.htm", records=cursor.fetchall(), title='User',)
except Exception as e:
return render_template("search.htm", records=str(e), title='User',)
return render_template('search.htm',
title='Cuckoo User Page',
)
templates/search.htm 模板/search.htm
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>{{ title }}</title>
<!-- Custom CSS -->
<link href="{{ url_for('static', filename='css/style.css') }}" />
<!-- Bootstrap -->
<link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<h1>{{ title }} - Search Page</h1>
<form class="navbar-form navbar-left" action="{{ url_for('search') }}" method="POST" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" name="search" value="{{ request.form.search }}">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
{{ records }}
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="{{ url_for('static', filename='js/bootstrap.min.js') }}"></script>
</body>
</html>
Issue Observed: Whenever i search something it is successfully fetched from db and iam storing it in logs.But when parsing the same to html page it is displaying as () 观察到的问题:每当我搜索某些东西时,都会成功地从db中获取它并将其存储在日志中。但是当将其解析为html页面时,它将显示为()
Logs: 日志:
2017-09-08 11:28:14,098 - app.views - INFO - Query is SELECT * from some where domain = 'akizekij.bovseter.org '
2017-09-08 11:28:14,098 - app.views - INFO - Data fetched is (('a', '134', '11333', 'akizekij.bovseter.org', '10.10', '13', datetime.date(2017, 2, 28), datetime.date(2017, 2, 28), '', 0L, 1L),)
Any suggestions on how to solve this? 关于如何解决这个问题有什么建议吗?
you are calling fetchall()
2 times which will return you empty tuple (or dict) for second. 您正在调用fetchall()
2次,这将使您fetchall()
返回空元组(或dict)。 on the ther hand logging the first fetch - and it show normally. 在另一手上记录第一个提取-并正常显示。
quick fix should be like below. 快速修复应如下所示。
data = cursor.fetchall()
logger.info('Data fetched is {}'.format(str(data)))
return render_template("search.htm", records=data, title='User',)
^^^^^^
edit 编辑
>>> import pymysql as mysql
>>> conn = mysql.connect(host='localhost', user='root', password='123456', database='demoapp')
<pymysql.connections.Connection object at 0x7f31e5973110>
>>> cur = conn.cursor()
>>> cur.execute("SELECT name FROM categories")
21
>>> d = cur.fetchall()
>>> d
(('Relationships',), ('Business',), ('Celebrity',), ('Events',), ('Fashion',), ('Law',), ('Fitness',), ('Medical',), ('MOVIES',), ('MUSIC',), ('Social',), ('Sport',), ('Tech',), ('Travel',), ('VIDEO',), ('Strains',), ('Food',), ('Education',), ('Activism',), ('Pets',), ('Headlines',))
>>> f = cur.fetchall()
>>> f
()
ref: https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/cursors.py 参考: https : //github.com/PyMySQL/PyMySQL/blob/master/pymysql/cursors.py
Lines 295, 74, 43 295、74、43行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.