flask render_template不会发送变量

时间:2017-10-28 21:47:33

标签: python python-3.x

所以这是我的代码

@auth.route("/",methods=["POST","GET"])
def index():
    form=LoginForm()

return render_template("index.html",form=form) 

这是我的login.html文件:

<form action="http://127.0.0.1:5000/auth/res" method="post">    
    {{form.csrf_token}}
    {{form.username}}
    {{form.password}}
    <input type="submit" value="submit" name="submit" />
    <form/>

这是我的路线:

    @auth.route("/res",methods=["POST","GET"])
def res ():
    form=LoginForm()
    if form.validate_on_submit():
         return render_template("admin.html")
    else: return render_template("index.html")

每当我运行我的应用程序时,我都会收到此错误:

* Running on http://127.0.0.1:5000/
127.0.0.1 - - [28/Oct/2017 22:41:10] "GET /auth/ HTTP/1.1" 200 -
127.0.0.1 - - [28/Oct/2017 22:41:13] "POST /auth/res HTTP/1.1" 500 -
Error on request:
Traceback (most recent call last):
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/werkzeug/serving.py", line 177, in run_wsgi
    execute(self.server.app)
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/werkzeug/serving.py", line 165, in execute
    application_iter = app(environ, start_response)
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/nidhal/Bureau/pp/app/auth/views.py", line 19, in res
    else: return render_template("index.html")
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/templating.py", line 128, in render_template
    context, ctx.app)
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/flask/templating.py", line 110, in _render
    rv = template.render(context)
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/nidhal/Bureau/pp/app/templates/index.html", line 5, in top-level template code
    {{form.csrf_token}}
  File "/home/nidhal/Bureau/pp/lib/python2.7/site-packages/jinja2/environment.py", line 397, in getattr
    return getattr(obj, attribute)
UndefinedError: 'form' is undefine

我一直在尝试,但我无法解决问题 我正在使用flask 0.10.1并使用python 2.7

1 个答案:

答案 0 :(得分:0)

我认为问题在于您没有将表单的变量传递到res()

内的index.html

这应该解决它:

@auth.route("/res",methods=["POST","GET"])
def res():
    form=LoginForm()
    if form.validate_on_submit():
         return render_template("admin.html")
    else: return render_template("index.html", form=form)