V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
gino
V2EX  ›  Python

请教jinja2的问题

  •  
  •   gino · 2012-10-16 00:39:48 +08:00 · 4464 次点击
    这是一个创建于 4225 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def render_template(filename, **context):
    extensions = context.pop('extensions', [])
    globals = context.pop('globals', {})

    jinja_env = Environment(
    loader = FileSystemLoader([os.path.join(os.path.dirname(__file__), 'templates'),'templates']),
    extensions = extensions,
    bytecode_cache = FileSystemBytecodeCache('/tmp', '%s.jinja2'),
    cache_size = 50,
    encoding = 'utf-8',
    autoescape = False,
    finalize = None,
    optimized = True,
    auto_reload = True, #None
    )
    jinja_env.globals.update(globals)
    return jinja_env.get_template(filename).render(context)


    ----
    def get(self):
    self.render_template('testjinja2.html',name='google')


    -----
    总是错误
    self.render_template('testjinja2.html',name='google')
    TypeError: render_template() takes exactly 1 argument (3 given)
    2 条回复    1970-01-01 08:00:00 +08:00
    linnchord
        1
    linnchord  
       2012-10-16 01:14:26 +08:00   ❤️ 1
    self.render_template

    这个self是从哪儿来的?

    要么定义
    def render_template(self, filename, **context):

    要么调用的时候不要通过self调用

    这是一个python函数使用问题,和jinja没有关系。
    gino
        2
    gino  
    OP
       2012-10-16 08:34:28 +08:00
    def render_template(self, filename, **context):

    我添加了self后,输出变成空白了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   979 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:46 · PVG 05:46 · LAX 14:46 · JFK 17:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.