代码之家  ›  专栏  ›  技术社区  ›  SaiKiran

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

  •  0
  • SaiKiran  · 技术社区  · 7 年前

    问题:

    代码:

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

    <!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>
    

    观察到的问题: 每当我搜索某个内容时,它都会成功地从数据库中获取,iam会将其存储在日志中。但在将其解析为html页面时,它显示为()

    日志:

    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),)
    

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  2
  •   marmeladze    7 年前

    fetchall() 2次,这将返回空元组(或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
    ()
    

    裁判: https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/cursors.py