在将此问题标记为dulipcate之前,我做了足够的研究 关于这个话题,但没有找到任何相关的解决方案,我知道有 堆栈上的类似问题,但它们与上下文不同 这个。
因此无法访问从html模板中的模板处理程序传递的变量,这是我的app处理程序,
class MconsoleHandler(web.RequestHandler):
def get(self):
# startrow = 0
if (self.get_argument('startrow', None, True) == None):
startrow = 0
print 'asasd'
else:
startrow = int(self.get_argument('startrow', None, True))
startrow = 0
self.render('templates/user_list.html', startrow=startrow)
我正在尝试访问startrow
中的变量templates/user_list.html
,如下所示,
<html>
<head>
{% if startrow==0 %}
//we give the value of the starting row to 0 because nothing was found in URL
{% set startrow = 0 %}
//otherwise we take the value from the URL
{% else %}
{% set startrow = startrow %}
{% end %}
</head>
</html>
但不断给出错误,这里是完整的追溯错误,
ERROR:tornado.application:Uncaught exception GET /main_console?startrow=23 (::1)
HTTPServerRequest(protocol='http', host='localhost:9000', method='GET', uri='/main_console?startrow=23', version='HTTP/1.1', remote_ip='::1', headers={'Accept-Language': 'en-US,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Host': 'localhost:9000', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:57.0) Gecko/20100101 Firefox/57.0', 'Connection': 'keep-alive', 'Cookie': '_xsrf=2|00d12adc|347904ed026ba7762d2460917f6b6cd3|1522069356', 'Cache-Control': 'max-age=0', 'Upgrade-Insecure-Requests': '1'})
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/tornado/web.py", line 1467, in _execute
result = method(*self.path_args, **self.path_kwargs)
File "/run/media/swap/a673bea3-9088-494e-bad5-5b80d6870b9b/home/swap/College Projects/Project Source/web_medicine/server.py", line 87, in get
self.render('user_list.html', startrow=startrow)
File "/usr/lib/python2.7/site-packages/tornado/web.py", line 719, in render
html = self.render_string(template_name, **kwargs)
File "/usr/lib/python2.7/site-packages/tornado/web.py", line 826, in render_string
return t.generate(**namespace)
File "/usr/lib/python2.7/site-packages/tornado/template.py", line 348, in generate
return execute()
File "user_list_html.generated.py", line 5, in _tt_execute
_tt_tmp = startrow # user_list.html:4
UnboundLocalError: local variable 'startrow' referenced before assignment
ERROR:tornado.access:500 GET /main_console?startrow=23 (::1) 10.21ms