Python Flask呈现模板未显示解析为html页面的消息?

Python Flask呈现模板未显示解析为html页面的消息?,python,mysql,python-2.7,flask,render,Python,Mysql,Python 2.7,Flask,Render,问题: @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(

问题:

@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',
                           )
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),)
Iam在search上有一个基于烧瓶的搜索栏,Iam从MySQL获取数据并希望显示在同一页面上

代码:

@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',
                           )
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),)
模板/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>


关于如何解决这个问题有什么建议吗?

您正在调用
fetchall()
2次,这将返回空的tuple(或dict)1秒。另一方面,记录第一次获取-并且它正常显示

快速修复应该如下所示

data = cursor.fetchall()
logger.info('Data fetched is {}'.format(str(data)))
return render_template("search.htm", records=data, title='User',)
                                            ^^^^^^ 
编辑

>>> 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
()
>将pymysql导入为mysql
>>>conn=mysql.connect(host='localhost',user='root',password='123456',database='demoapp')
>>>cur=连接光标()
>>>当前执行(“从类别中选择名称”)
21
>>>d=cur.fetchall()
>>>d
(‘关系’,(‘商业’,),(‘名人’,),(‘事件’,),(‘时尚’,(‘法律’,),(‘健身’,(‘医疗’,),(‘电影’,),(‘音乐’,),(‘社交’,),(‘体育’,(‘科技’,),(‘旅游’,(‘视频’,),(‘品系’,(‘食品’,(‘教育’,(‘运动’,),(‘宠物’,),(‘头条’,),),(‘头条’,)
>>>f=cur.fetchall()
>>>f
()
参考:


第295、74、43行

您正在调用
fetchall()
两次,这将返回空tuple(或dict)一秒钟。另一方面,记录第一次获取-并且它正常显示

快速修复应该如下所示

data = cursor.fetchall()
logger.info('Data fetched is {}'.format(str(data)))
return render_template("search.htm", records=data, title='User',)
                                            ^^^^^^ 
编辑

>>> 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
()
>将pymysql导入为mysql
>>>conn=mysql.connect(host='localhost',user='root',password='123456',database='demoapp')
>>>cur=连接光标()
>>>当前执行(“从类别中选择名称”)
21
>>>d=cur.fetchall()
>>>d
(‘关系’,(‘商业’,),(‘名人’,),(‘事件’,),(‘时尚’,(‘法律’,),(‘健身’,(‘医疗’,),(‘电影’,),(‘音乐’,),(‘社交’,),(‘体育’,(‘科技’,),(‘旅游’,(‘视频’,),(‘品系’,(‘食品’,(‘教育’,(‘运动’,),(‘宠物’,),(‘头条’,),),(‘头条’,)
>>>f=cur.fetchall()
>>>f
()
参考:

第295、74、43行