我的烧瓶应用程序一直存在缩进问题
特别是当我尝试在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
答案 0 :(得分: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)