我的Jinja2模板应该从我的索引页面自动化HTML结构。运行和浏览程序时,浏览器上没有显示任何内容。当我使用一个变量时DID工作,但是当我嵌套几个时出现问题。这是代码:
class MainPage(Handler):
def get(self):
stage = [name, lessons]
name = 'Stage 1'
lessons = [
['Lesson 1 Important Concepts',[
['HTML', 'HTML is the ...'],
['Tags and Elements', 'This is an e...'],
['Why Computers Are Stupid', 'Computers ne...'],
['Inline and Block Elements', 'Block elem...']
]
]
['Lesson 2: Creating a Structured Document with HTML',[
['Developer Tools','Everything design...'],
['HTML is Structured Like a Family Tree','HTML..'],
['Text Editors','HTML is writt....'],
]
]
['Lesson 3: Adding Style to HTML Using CSS',[
['Avoiding Repetition','If programm...'],
['CSS','CSS, or cascading style sh....'],
]
]
]
self.render("index.html", name=name, lessons=lessons)
以下是模板:
<h1> {{name}} Important Concepts</h1>
{% for lessons in stage %}
<div class=“lesson”>
<h2>{{ lessons[0] }}</h2>
<div class="concept”>
<div class="concept-title">
{{ lessons[0][0] }}
</div>
<div class="concept-description">
{{ lessons[0][1] }}
</div>
</div>
</div>
{% endfor %}
我的GitHub项目在这里:https://github.com/graceehayden/Stage4Udacity-Session-2
我得到的错误是:
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/gracehayden/Desktop/Udacity/Stage4Project/main.py", line 22, in get
stage = [name, lessons]
UnboundLocalError: local variable 'name' referenced before assignment
答案 0 :(得分:0)
值只能在 之后使用 。因此,而不是:
stage = [name, lessons] # this fails because "name" and "lessons" aren't yet set
name = 'Stage 1'
lessons = [] # big value here
重新排序这些行,将name
和lessons
(定义其值)的定义放在这些值用于定义stage
的位置之前:
name = 'Stage 1'
lessons = [] # big value here
stage = [name, lessons]