Question

Not sure why I am getting this error

Here is the section in my definition that is defining my variable messages_outbox

@view_config(route_name="dashboard", request_method='GET')
def home(self, is_desktop=True):
    callback = self.session["bounce"]
    if callback is not None:
        self.session["bounce"] = None
        return self.redirect(callback)

    mak = Dashboard.DesktopHomeMak

    member = self.member

    dict = {}
    # self.cls.append_preview(member=member, dict=dict)
    state = member.state

    messages_outbox = self.ctx.messaging.outbox
    messages_outbox = messages_outbox[:3] if len(messages_outbox) > 3 else messages_outbox
    messages_outbox_total = len(messages_outbox)

    messages_inbox = self.ctx.messaging.inbox
    messages_inbox = messages_inbox[:3] if len(messages_inbox) > 3 else messages_inbox
    messages_inbox_total = len([m for m in messages_inbox])

    dict['messages_outbox'] = messages_outbox
    dict['messages_outbox_total'] = messages_outbox_total

    dict['messages_inbox'] = messages_inbox
    dict['messages_inbox_total'] = messages_inbox_total

    newbie = self.ctx.session.newbie
    profile = self.member.profile
    profile = profile.deflate()
    profile["avatar"] = self.avatar_uri_large
    dict['profile'] = profile
    dict['newbie'] = newbie
    dict['verified'] = None
    dict['member'] = member

    if newbie is True:
        session = self.ctx.session
        try:
            state = session.state
            state.pop("newbie")
        except:
            pass

        try:
            session.save_state()
        except:
            pass

    ## if the member has not verified their email address, let them know
    if self.member.is_verified is False:
        dict['verified'] = False
    else:
        dict['verified'] = True

    print dict['messages_outbox']
    print messages_outbox

    return self.render(mak, value=dict, messages_outbox=messages_outbox, messages_inbox=messages_inbox)

The mak file(base.mak) is a template that pulls in another template file(home.mak) which contains the code below that is throwing the error (on the first line):

%if messages_outbox:
    % for messages_outbox in messages_outbox:
        % if messages_outbox["type"] == "Introduction":
            ${makerow_outbox(messages_outbox)}
        % endif
    % endfor

    <thead>
        <tr>
            <td colspan="6">
                <p class="message">You don’t have any active requests.  To make a request use the <a href="#" class="search-highlight">search field</a></p>
            </td>
        </tr>
    </thead>
%endif

What I don't understand is that clearly messages_outbox is defined before I render the templates, so why am I getting this error?


Full Traceback:

Traceback (most recent call last):
File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/pyramid-1.4.2-py2.7.egg/pyramid/mako_templating.py", line 232, in __call__
result = template.render_unicode(**system)
File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/template.py", line 452, in render_unicode
as_unicode=True)
File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/runtime.py", line 783, in _render
**_kwargs_for_callable(callable_, data))
File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/runtime.py", line 815, in _render_context
_exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
File "/Applications/MAMP/htdocs/WhoAt/env/lib/python2.7/site-packages/Mako-0.8.1-py2.7.egg/mako/runtime.py", line 841, in _exec_template
callable_(context, *args, **kwargs)
File "/Applications/MAMP/htdocs/WhoAt/env/www/www/templates/index/desktop/base_dashboard.mak", line 647, in render_body
${next.body()}
File "/Applications/MAMP/htdocs/WhoAt/env/www/www/templates/index/desktop/home.mak", line 81, in render_body
%if messages_outbox:
UnboundLocalError: local variable 'messages_outbox' referenced before assignment
Was it helpful?

Solution

You should change home.mak so you don't mask messages_outbox:

%if messages_outbox:
    % for outbox in messages_outbox:
        % if outbox["type"] == "Introduction":
            ${makerow_outbox(outbox)}
         ...

Also, this is terrible: dict = {}. You shouldn't use names that reference Python builtin functions. It will cause you a lot of frustration later. Maybe rename it outbox_dict.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top