不确定我收到此错误的原因
以下是我的定义中定义变量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)
mak文件(base.mak)是一个模板,它引入另一个模板文件(home.mak),其中包含抛出错误的下面代码(在第一行):
%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
我不明白的是,在我渲染模板之前明确messages_outbox
已定义,为什么我会收到此错误?
完整追溯:
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
答案 0 :(得分:1)
您应该更改home.mak,这样您就不会屏蔽messages_outbox:
%if messages_outbox:
% for outbox in messages_outbox:
% if outbox["type"] == "Introduction":
${makerow_outbox(outbox)}
...
此外,这很可怕:dict = {}
。您不应该使用引用Python内置函数的名称。这会让你后来感到非常沮丧。也许将其重命名为outbox_dict
。