推荐学习书目
› 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 · Oct 16, 2012 · 5628 views
    This topic created in 5098 days ago, the information mentioned may be changed or developed.
    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 replies  •  1970-01-01 08:00:00 +08:00
    linnchord
        1
    linnchord  
       Oct 16, 2012   ❤️ 1
    self.render_template

    这个self是从哪儿来的?

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

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

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

    我添加了self后,输出变成空白了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   808 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:26 · PVG 04:26 · LAX 13:26 · JFK 16:26
    ♥ Do have faith in what you're doing.