Jinja2无法使用webapp2呈现数据html问题

时间:2012-09-19 09:25:17

标签: python jinja2 webapp2

在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什么都不返回,无法返回那个取消的数据,代码有什么问题,有人可以帮我解决吗? 非常非常感谢:(

1 个答案:

答案 0 :(得分: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/