terminal-labs / lektor-jinja-content

Render Lektor content fields with Jinja2.
Other
11 stars 5 forks source link

RuntimeError: No context found #6

Closed paternal closed 6 years ago

paternal commented 6 years ago

When I use the url filter, I get a python exception : RuntimeError: No context found.

How to reproduce bug

Started build
Finished build in 0.00 sec
Traceback (most recent call last):
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/lektor/devserver.py", line 47, in build
    builder.build_all()
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/lektor/builder.py", line 1141, in build_all
    prog, build_state = self.build(source, path_cache=path_cache)
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/lektor/builder.py", line 1110, in build
    source=source, prog=prog)
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/lektor/pluginsystem.py", line 161, in emit
    rv[plugin.id] = handler(**kwargs)
  File "/home/louis/.cache/lektor/packages/c724b46bbb0067f95187a3f79b0aafaf/lektor_jinja_content.py", line 33, in on_before_build
    prog.source._data[field].source = self.jinjaify(pad, prog.source, data.source, field)
  File "/home/louis/.cache/lektor/packages/c724b46bbb0067f95187a3f79b0aafaf/lektor_jinja_content.py", line 19, in jinjaify
    data = self.env.jinja_env.from_string(data).render(context)
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "<template>", line 7, in top-level template code
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/lektor/environment.py", line 446, in <lambda>
    lambda ctx, *a, **kw: url_to(*a, **kw)),
  File "/home/louis/.virtualenvs/ababsurdo/lib/python3.6/site-packages/lektor/context.py", line 15, in url_to
    raise RuntimeError('No context found')
RuntimeError: No context found

Environment

Everything is done inside a virtualenv (which, I know, is discouraged, but I do not think it is the cause of the issue here), on Debian testing (buster).

$ python --version
Python 3.6.6
$ lektor --version
Lektor, version 3.1.1
$ pip --version
pip 10.0.1
$ lektor plugins list
jinja-content (version 0.3)
nixjdm commented 6 years ago

Thanks for finding this, and sorry for the bug. The error is definitely something in this code. I think I left something out when constructing the context (as the error says). Once fixed there should be a test for this as well.

nixjdm commented 6 years ago

This should be fixed now (and tests written) in 0.4.1 that I just released.

paternal commented 6 years ago

Great! It works. Now I can ditch this poorly designed, quickly written, badly broken extension that I wrote and replace it with jinja2 macros and filters. Thanks!