2013-11-22 56 views
0

我一直有我的燒瓶應用程序的縮進問題。
特別是當我嘗試在返回語句之前聲明變量時。另外,當我嘗試在return語句之前使用session['logged_in] = True時。燒瓶壓痕的縮進錯誤

試圖尋找任何跡象表明我錯誤地構造了這個,我錯過了什麼?

from app import app 
from flask import render_template, url_for, request, url_for, redirect, session 
from form import NewUser 

@app.route('/') 
def home(): 
    return render_template('home.html') 

@app.route('/logout') 
def logout(): 
    session.pop('logged_in', None) 
    redirect(url_for('home')) 

@app.route('/login', methods=['POST', 'GET']) 
def login(): 
    form = NewUser() 

    if request.method == 'POST': 
     if request.form['username'] != 'admin' and request.form['password'] != 'password': 
      error = "uername/password incorrect" 
      return render_template('login.html', form=form, error=error) 
     else: 
      session['logged_in'] = True 
      return redirect(url_for('secretarea')) 
    return render_template('login.html', form=form, error=error) 


@app.route('/secretarea') 
def secretarea(): 
    if session['logged_in'] = True: 
     return "this area is a secret" 
    else: 
     error="please log in" 
     return render_template('login.html', error=error) 

我得到的錯誤是這個

File "runserver.py", line 2, in <module> 
    from app import app 
    File "/home/roman/flask/myblog/app/__init__.py", line 9, in <module> 
    from app import views 
    File "/home/roman/flask/myblog/app/views.py", line 21 
    return render_template('login.html', form=form, error=error) 
    ^
    IndentationError: unexpected indent 
+2

2件事 - 1.確保您不混合製表符和空格。 2.您在'logout'端點忘記了'return'語句 – karthikr

回答

0

您的登錄看法是不正確的結構。在最後一個返回行中,您嘗試傳遞您在if條件中定義的'error'變量。你也不需要兩次返回login.html模板。我將重新排列視圖爲:

@app.route('/login', methods=['POST', 'GET']) 
def login(): 
    form = NewUser() 
    error='' # initialize the error variable outside the if loop 
    if request.method == 'POST': 
     if request.form['username'] != 'admin' and request.form['password'] != 'password': 
      error = "uername/password incorrect" 
     else: 
      session['logged_in'] = True 
      return redirect(url_for('secretarea')) 

    # This line will execute if GET request OR the username/password checks fails 
    return render_template('login.html', form=form, error=error) 
+1

謝謝您的回答。我沒有足夠的代表給你一個upvote,但會回來,當我做,歡呼! – shoemonkey