0

在 main.py 代码中:

# -*- coding: utf-8 -*-

import os
import time
import re
import urllib
import urllib2
import MySQLdb

import webapp2
import jinja2

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
            autoescape = True)


class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)

    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)

    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))


class MainPage(Handler):
    def get(self):
        self.render('front.html')

    def post(self):
        hostip = self.request.get('hostip')
        username = self.request.get('username')
        dbname = self.request.get('dbname')
        password = self.request.get('password')

        if hostip and username and dbname and password:
            conn = MySQLdb.connect(host = hostip, user = username, passwd = password, db = dbname)
            cur = conn.cursor()
            cur.execute("show tables")
            alltable = cur.fetchall()
            tablenames = []
            for i in range(len(alltable)):
                for j in range(len(alltable[i])):
                    tablenames.append(alltable[i][j])
            finalname = tablenames


app = webapp2.WSGIApplication([('/', MainPage)],
                                debug = True)

def main():
    from paste import httpserver
    httpserver.serve(app, host = '127.0.0.1', port = '8888')

if __name__ == '__main__':
    main()

在 front.html 中:

<html>
<body>
    <h1>ACCESS YOUR SQL</h1>

    <form method = "post" accept-charset="UTF-8" action="/"/>
        <label>
            <div>SQL Host IP</div>
            <input type="text" name="hosttip" value="{{hostip}}">
        </label>

        <label>
            <div>UserName</div>
            <input type="text" name="username" value="{{username}}">
        </label>

        <label>
            <div>DBname</div>
            <input type="text" name="dbname" value="{{dbname}}">
        </label>

        <label>
            <div>Password</div>
            <input type="password" name="password" value="{{password}}">
        </label>

        <input type="submit" value="submit">
    <form>

    <hr>

    {% for tablename in finalname %}

        <form>
            <input type="checkbox" name="tablename">{{tablename}}

        </form>
    {% endfor %}

</body>
</html>

填充并运行后,front.html什么都没有返回,无法返回fetchall的数据,代码有什么问题,有人可以帮我解决吗?非常非常非常感谢:(

4

1 回答 1

0

渲染页面时:

self.render('front.html')

您还必须将您在页面中引用的数据传递给它

尝试这个

from flask import render_template

然后用数据渲染你的页面

return render_template('front.html',hostip = hostip, username = username)

现在你可以使用

{{ hostip }}

并访问您在 render_template 调用中作为 hostip 传递的任何内容。

此处有关该用法的信息:http: //jinja.pocoo.org/docs/templates/#variables

最简单的用法就是这样:

>>> from jinja2 import Template
>>> template = Template('Hello {{ name }}!')
>>> template.render(name='John Doe')
u'Hello John Doe!'

http://jinja.pocoo.org/docs/intro/

于 2012-09-19T09:35:00.337 回答