如何将自定义对象传递给Jinja模板?

时间:2016-03-22 14:46:25

标签: python python-sphinx read-the-docs

我的基于Sphinx的文档有一个带有自定义Jinja2模板的特殊页面。我想传递使用一些Python代码生成的自定义对象作为此模板的上下文。 (对于各种日历,他们会datetime。)conf.py包括以下内容:

import datetime

def add_index(app):
    yield 'index', {'today': datetime.datetime.today()}, 'index.html'

def setup(app):
    app.connect('html-collect-pages', add_index)

这在本地构建良好,但阅读文档尝试使用JSON序列化它。 有没有办法通过"非JSON-able"对象到模板?

我得到的例外是:

Running Sphinx v1.3.5
making output directory...
loading translations [cs]... done
loading pickled environment... not yet created
loading intersphinx inventory from http://docs.python.org/objects.inv...
building [mo]: targets for 0 po files that are out of date
building [json]: targets for 1 source files that are out of date
updating environment: 1 added, 0 changed, 0 removed
reading sources... [100%] course/index

looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] course/index

generating indices... genindex

Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/cmdline.py", line 244, in main
    app.build(opts.force_all, filenames)
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/application.py", line 266, in build
    self.builder.build_update()
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/__init__.py", line 251, in build_update
    'out of date' % len(to_build))
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/__init__.py", line 325, in build
    self.finish()
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/html.py", line 456, in finish
    self.finish_tasks.add_task(self.gen_additional_pages)
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/util/parallel.py", line 40, in add_task
    res = task_func()
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/html.py", line 482, in gen_additional_pages
    self.handle_page(pagename, context, template)
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/html.py", line 1088, in handle_page
    self.dump_context(ctx, outfilename)
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/builders/html.py", line 1070, in dump_context
    self.implementation.dump(context, f, *self.additional_dump_args)
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/util/jsonimpl.py", line 28, in dump
    return json.dump(obj, fp, *args, **kwds)
  File "/usr/lib/python3.4/json/__init__.py", line 178, in dump
    for chunk in iterable:
  File "/usr/lib/python3.4/json/encoder.py", line 422, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.4/json/encoder.py", line 396, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.4/json/encoder.py", line 429, in _iterencode
    o = _default(o)
  File "/home/docs/checkouts/readthedocs.org/user_builds/pyladiescz/envs/latest/lib/python3.4/site-packages/sphinx/util/jsonimpl.py", line 23, in default
    return json.JSONEncoder.default(self, obj)
  File "/usr/lib/python3.4/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2016, 3, 22, 14, 37, 54, 816952) is not JSON serializable

Exception occurred:
  File "/usr/lib/python3.4/json/encoder.py", line 173, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2016, 3, 22, 14, 37, 54, 816952) is not JSON serializable
The full traceback has been saved in /tmp/sphinx-err-wt0rbhbk.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!

0 个答案:

没有答案