0

我是python和flask的初学者。我想将一个列表从.py文件传递给视图文件并这样做:

蟒蛇文件:

 @app.route('/admin',methods=['GET'])

 def panelSettings():   
     myList = ["gan","dalf"]
     return render_template('admin.html',list=myList)

.html 文件:

my list's first eleman is:{{ list[0] }} 

但是这段代码不起作用。它说:

 myList is not defined

我的目的是将多个或更多变量传递给视图。我该怎么办?

我的追溯是:

Traceback (most recent call last):
  File "/Users/ozcan/flask/flask/app.py", line 1823, in __call__
   return self.wsgi_app(environ, start_response)
File "/Users/ozcan/flask/flask/app.py", line 1811, in wsgi_app
  response = self.make_response(self.handle_exception(e))
File "/Users/ozcan/flask/flask/app.py", line 1809, in wsgi_app
  response = self.full_dispatch_request()
File "/Users/ozcan/flask/flask/app.py", line 1482, in full_dispatch_request
  rv = self.handle_user_exception(e)
File "/Users/ozcan/flask/flask/app.py", line 1480, in full_dispatch_request
  rv  = self.dispatch_request()
File "/Users/ozcan/flask/flask/app.py", line 1466, in dispatch_request
  return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/ozcan/Documents/python/app.py", line 104, in panel
  return render_template("panel.html",route=route)
File "/Users/ozcan/flask/flask/templating.py", line 127, in render_template
  context, ctx.app)
File "/Users/ozcan/flask/flask/templating.py", line 109, in _render
  rv = template.render(context)
File "/Users/ozcan/flask/venv/lib/python2.7/site-packages/Jinja2-2.6-  py2.7.egg/jinja2/environment.py", line 894, in render
  return self.environment.handle_exception(exc_info, True)
File "/Users/ozcan/Documents/python/templates/panel.html", line 1, in top-level template code
  {% extends "layout.html" %}
File "/Users/ozcan/Documents/python/templates/layout.html", line 62, in top-level template code
  {%block panel%}{%endblock%}
File "/Users/ozcan/Documents/python/templates/panel.html", line 11, in block "panel"
  {{ list[0]}}
File "/Users/ozcan/flask/venv/lib/python2.7/site-packages/Jinja2-2.6-py2.7.egg/jinja2/environment.py", line 353, in getitem
  return obj[argument]
UndefinedError: 'list' is undefined
4

1 回答 1

3

您不能list用作变量的名称,因为list它是 Python 中的保留关键字。

于 2013-02-15T22:40:40.980 回答