将数组传递给烧瓶中的视图

时间:2013-02-11 21:27:59

标签: python flask

我是python和flask的初学者。我想将一个列表从.py文件传递给视图文件并执行此操作:

python文件:

 @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

1 个答案:

答案 0 :(得分:3)

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