Issue #6 resolved

'errors' not defined in wheezy.template preprocessor.py

Kenny Hong
created an issue

hi:

We have been going the guestbook tutorials and got stuck when converting from using mako template to wheezy.template. The layout.html and list.html worked, however for add.html we reduced to just one statement:

@greeting.error()

kept getting 'errors' not defined in preprocessor.py

These are our changes to config.py

from wheezy.html.ext.template import WhitespaceExtension
from wheezy.html.ext.template import WidgetExtension
from wheezy.html.utils import format_value
from wheezy.html.utils import html_escape
from wheezy.template.engine import Engine
from wheezy.template.ext.core import CoreExtension
from wheezy.template.loader import FileLoader
from wheezy.template.preprocessor import Preprocessor
from wheezy.web.templates import WheezyTemplate
from wheezy.template.ext.determined import DeterminedExtension


def runtime_engine_factory(loader):
    engine = Engine(
        loader=loader,
        extensions=[
            CoreExtension(),
            WidgetExtension(),
            WhitespaceExtension(),
        ])
    engine.global_vars.update({
        'format_value': format_value,
        'h': html_escape,
    })
    return engine

searchpath = ['templates']
engine = Engine(
    loader=FileLoader(searchpath),
    extensions=[
        CoreExtension('#', line_join=None),
        DeterminedExtension(['path_for', '_']),
    ])

engine = Preprocessor(runtime_engine_factory, engine,
                      key_factory=lambda ctx: ctx['locale'])
options = {
    'render_template': WheezyTemplate(engine)
}

... and layout.html:

<html>
    <head>
        <title>Guestbook</title>
        <link href="@path_for('static', path='site.css')"
            type="text/css" rel="stylesheet" />
        <script type="text/javascript"
        src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
        </script>
        <script type="text/javascript"
        src="@path_for('static', path='site.js')"></script>
    </head>
    <body>
        <div id="main">
            @def body():
            @end
            @body()
        </div>
    </body>
</html>

...and add.html:

@extends("layout.html")

@def body():
@require(greeting)
<h1>Sign Guestbook</h1>
@greeting.error()
<form action="@path_for('add')" method='post'>
    <p>
    @greeting.author.label('Author:')
    @greeting.author.textbox()
    @greeting.author.error()
    </p>
    <p>
    @greeting.message.textarea()
    @greeting.message.error()
    </p>
    <p>
    <input type='submit' value='Leave Message'>
    </p>
</form>

<script type="text/javascript">
    $(document).ready(function() {
        ajaxForm();
    })
</script>
@end

Error output:

(wheezy-env)root@beta-virtual-machine:~/wheezy/tutorials/guestbook2# python app.py 
/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.web-0.1-py2.7.egg/wheezy/web/middleware/bootstrap.py:31: UserWarning: Ticket: digestmod is not specified, fallback to sha1
  options['ticket'] = Ticket()
/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.web-0.1-py2.7.egg/wheezy/web/middleware/bootstrap.py:31: UserWarning: Ticket: cypher not available
  options['ticket'] = Ticket()
Visit http://localhost:8080/
Traceback (most recent call last):
  File "/root/opt/Python-2.7.4/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.http-0.1.297-py2.7.egg/wheezy/http/application.py", line 69, in __call__
    response = self.middleware(request)
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.http-0.1.297-py2.7.egg/wheezy/http/application.py", line 20, in <lambda>
    return lambda request: func(request, following)
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.http-0.1.297-py2.7.egg/wheezy/http/middleware.py", line 51, in __call__
    response = following(request)
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.http-0.1.297-py2.7.egg/wheezy/http/application.py", line 20, in <lambda>
    return lambda request: func(request, following)
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.web-0.1-py2.7.egg/wheezy/web/middleware/routing.py", line 25, in __call__
    return handler(request)
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.web-0.1-py2.7.egg/wheezy/web/handlers/method.py", line 16, in __new__
    return handler()
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.web-0.1-py2.7.egg/wheezy/web/handlers/method.py", line 27, in __call__
    response = self.get()
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.http-0.1.297-py2.7.egg/wheezy/http/cache.py", line 34, in cache
    response = handler(request, *args, **kwargs)
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.web-0.1-py2.7.egg/wheezy/web/transforms.py", line 17, in single_strategy
    factory(handler, *args, **kwargs))
  File "/root/wheezy/tutorials/guestbook2/views.py", line 42, in get
    return self.render_response('add.html', greeting=greeting)
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.web-0.1-py2.7.egg/wheezy/web/handlers/base.py", line 130, in render_response
    template_name, dict(self.helpers, **kwargs)))
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.web-0.1-py2.7.egg/wheezy/web/templates.py", line 123, in __call__
    return self.render(template_name, kwargs, {}, {})
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.template-0.1.135-py2.7.egg/wheezy/template/preprocessor.py", line 41, in render
    return runtime_engine.renders[name](ctx, local_defs, super_defs)
  File "add.html", line 53, in render
  File "/root/.virtualenvs/wheezy-env/lib/python2.7/site-packages/wheezy.template-0.1.135-py2.7.egg/wheezy/template/preprocessor.py", line 68, in render
    return engine.renders[name](ctx, local_defs, super_defs)
  File "layout.html", line 16, in render
  File "add.html", line 6, in body
NameError: global name 'errors' is not defined
127.0.0.1 - - [22/May/2013 13:02:29] "GET /add HTTP/1.1" 500 59

I'm also wondering with our list.html:

@extends("layout.html")

@def body():
@require(greetings)
<h1>Guestbook</h1>
<a href="@path_for('add')">Sign guestbook</a>
@for g in greetings:
    <p>
    @g.id!s. On @g.created_on.strftime('%m/%d/%Y %I:%M %p'),    
    <b>
    @if g.author:
        @g.author
    @else:
    'anonymous'
    @endif    
    </b> wrote:   
    <blockquote>@g.message.replace('\n', '<br/>')</blockquote>
    </p>
@end
@end

is there a wheezy.template syntax similar to mako's one line:

${g.author or 'anonymous'}

We are eventually going to use wheezy.web with a nosql database + i18n internationalization.

Thanks in advance for helping out.

Kind Regards

Juan

Comments (1)

  1. Andriy Kornatskyy repo owner

    The errors is context variable supplied to template engine by base handler. In wheezy.template you need to be explicit about any context variables used, thus you need to

    @require(greeting, errors, path_for)
    

    The list of context variables is here (they are under helpers): http://pythonhosted.org/wheezy.web/userguide.html#templates

    is there a wheezy.template syntax similar to mako's one line:

    ${g.author or 'anonymous'}

    Use the following workaround by str function call:

    @str(g.author or 'anonymous')
    

    you can add to template engine global variable mapping 's': str and short expression just to @s(...) but that will be less obvious.

  2. Log in to comment