【发布时间】:2018-06-19 01:41:28
【问题描述】:
我有一个非常简单的烧瓶表单设置,带有 1 个字段。该字段通过 POST 请求传递给脚本,该脚本运行并将 render_template 返回到结果页面,并从脚本传递 4 个列表对象。
我可以在 python 解释器中运行脚本并打印列表的值。但是,它似乎没有将列表传递回视图。我不断收到表单未定义的错误。我认为它只是跳到索引路由中的返回函数,而不是从函数返回视图。在发送到视图之前,我需要对列表变量做些什么吗?
路线
@app.route('/', methods=['GET', 'POST'])
def index():
form = Search()
if form.validate_on_submit():
name=request.form['name']
smoothSearch(name)
return render_template("index.html", form=form)
功能
#function runs a for loop appending items to each list item then returns a render_template shown below
else:
return render_template("results.html", usernames=usernames, userHandle=userHandle, userText=userText, postTime=postTime)
编辑以在下面包含 jinja2 引用
jinja2.exceptions.UndefinedError jinja2.exceptions.UndefinedError: 'form' 未定义
Traceback(最近一次调用最后一次) 调用中的文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py”,第 1997 行 return self.wsgi_app(environ, start_response)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py”,第 1985 行,在 wsgi_app response = self.handle_exception(e)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py”,第 1540 行,在 handle_exception 中 reraise(exc_type, exc_value, tb)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/_compat.py”,第 33 行,在 reraise 提升价值
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py”,第 1982 行,在 wsgi_app 响应 = self.full_dispatch_request()
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py”,第 1614 行,在 full_dispatch_request rv = self.handle_user_exception(e)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py”,第 1517 行,在 handle_user_exception reraise(exc_type, exc_value, tb)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/_compat.py”,第 33 行,在 reraise 提升价值
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py”,第 1612 行,在 full_dispatch_request rv = self.dispatch_request()
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/app.py”,第 1598 行,在 dispatch_request 返回 self.view_functionsrule.endpoint
文件“/home/user/temp/code-projects/smoothSearch/app.py”,第 22 行,在索引中 平滑搜索(名称)
文件“/home/user/temp/code-projects/smoothSearch/smoothSearch.py”,第 31 行,在 smoothSearch 中 return render_template('results.html', usernames=usernames, userHandle=userHandle, userText=userText, postTime=postTime)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/templating.py”,第 134 行,在 render_template 上下文,ctx.app)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/flask/templating.py”,第 116 行,在 _render rv = template.render(context)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/jinja2/environment.py”,第 1008 行,在渲染中 return self.environment.handle_exception(exc_info, True)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/jinja2/environment.py”,第 780 行,在 handle_exception reraise(exc_type, exc_value, tb)
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/jinja2/_compat.py”,第 37 行,在 reraise raise value.with_traceback(tb)
顶级模板代码中的文件“/home/user/temp/code-projects/smoothSearch/templates/results.html”,第 1 行 {% 扩展“index.html”%}
顶级模板代码中的文件“/home/user/temp/code-projects/smoothSearch/templates/index.html”,第 17 行 {{ form.csrf_token }}
文件“/home/user/temp/code-projects/smoothSearch/lib/python3.5/site-packages/jinja2/environment.py”,第 430 行,在 getattr 返回 getattr(obj, 属性)
jinja2.exceptions.UndefinedError: 'form' 未定义
【问题讨论】: