2017-10-28 82 views
0

,所以這是我的代碼燒瓶render_template將不會發送varriables

@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 

,我一直在努力,但我只是無法獲得武漢理工大學是在萬阿英,蔣達清 和我使用燒瓶0.10.1和使用Python 2.7

+0

爲什麼在'指數return語句()'不縮進? – gommb

+0

對不起,這是miastake,當我在這裏複製我的代碼xD,但我的代碼是好的 –

回答

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) 
+0

thnx這固定它xD,但更多的問題是,vlidate_on_submit是不是真的? –

+0

你從哪裏得到'LoginForm()'? – gommb