def login(): if current_user.is_authenticated: #如果用戶已經被認證過了 return redirect(url_for('index')) #直接回首頁不需要進行後面的驗證程序 title="登入頁面" form=LoginForm() #原本是csrf_enabled=False寫了才不會暴 後期改寫到forms.py內 if form.validate_on_submit(): #如果form是透過submit點進來的 #msg="username={},password={},remember_me={}".format( # form.username.data, # form.password.data, # form.remember_me.data # ) #把三個參數直接寫在字串裡面的寫法 #print(msg) u=User.query.filter_by(username=form.username.data).first() #根據suername字串去filter_by出來只有有或沒有 first()第一個結果 if u is None or not u.check_password(form.password.data) : #如果 沒有這個用戶或 密碼不符合返回 login畫面 print('invalid username or password') return redirect(url_for('login')) #否則就進入index 通過驗證的u ,根據用戶勾選是否記憶 login_user(u,remember=form.remember_me.data) next_page = request.args.get('next') # request方法 獲取登入前的頁面帶的next參數 if next_page: #如果有帶參數 代表從別的頁面跳入登陸頁 return redirect(next_page) #登入後返回原頁 #return redirect('/') #路徑回根目錄 return redirect(url_for('index')) #改用url_for(函式名)的方式連結 萬一__init__.py改路徑才不用一個一個改 return render_template('login.html', title=title, form=form)
def login(): if current_user.is_authenticated: return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): # msg = 'username ={}, password={},remember_me={}'.format( # form.username.data, # form.password.data, # form.remember_me.data # ) # print(msg) u = User.query.filter_by(username=form.username.data).first() if u is None: print('Username invalid') return redirect(url_for('login')) if u.check_password(form.username.data): login_user(u, remember=form.remember_me.data) next_page = request.args.get('next') if next_page: return redirect(next_page) return redirect(url_for('index')) else: print('Password invalid') return redirect(url_for('login')) return render_template('login.html', title='Sign in', form=form)
def login(): # form = LoginForm(csrf_enabled=False) # 初学暂时不需要考虑csrf安全问题 参数已过时 form = LoginForm(meta={'csrf': False}) # 上行参数已过时 替换新参数 if form.validate_on_submit(): msg = "username={}, password={}, remember_me={}".format( form.username.data, form.password.data, form.remember_me.data) print(msg) return redirect(url_for('index')) return render_template('login.html', title="Sign In", form=form)
def login(): form = LoginForm(csrf_enabled=False) if form.validate_on_submit(): msg = "username={},password={},remember_me={}".format( form.username.data, form.password.data, form.remember_me.data ) print(msg) return redirect(url_for('index')) return render_template('login.html',title="登录",form=form)
def login(): if current_user.is_authenticated: return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): u = User.query.filter_by(username=form.username.data).first() if u is None or not u.check_password(form.password.data): print('invalid username or password') return redirect(url_for('login')) login_user(u, remember=form.remember_me.data) next_page = request.args.get('next') if next_page: return redirect(next_page) return redirect(url_for('index')) return render_template('login.html', title="登录", form=form)
def login(): if current_user.is_authenticated: # 如果当前用户已经登录 return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): # validate_on_submit() 如果能通过所有验证 u = User.query.filter_by(username=form.username.data).first() if u is None or not u.check_password(form.password.data): # check the username and password flash('Invalid username or password') return redirect(url_for('login')) login_user(u, remember=form.remember_me.data) # log the user in next_page = request.args.get('next') # 导入了flask中的request,获取下一页的重定向 if not next_page or url_parse(next_page).netloc != '': return redirect(url_for('index')) # redirect 重定向 # url_for: find the url which is bind with index function return redirect(next_page) return render_template('login.html', title="Sign In", form=form)
def login(): if current_user.is_authenticated: return redirect(url_for('index')) # form1 = Login_Form(csrf_enabled = False) form1 = LoginForm() if form1.validate_on_submit(): u = User.query.filter_by(username=form1.username.data).first() if u is None or not u.check_password( form1.password.data): #如果账号不存在或者密码不正确 print('invalid username or passwod') return redirect(url_for('login')) login_user(u, remember=form1.remember_me.data) next_page = request.args.get( 'next') #作用:在登陆之前可能会点其他入口的页面,在登陆之后会直接跳转到那个页面 if next_page: return redirect(next_page) return redirect(url_for('index')) return render_template('login.html', title='sign in', form=form1)
def login(): # 判断用户是否登录 if current_user.is_authenticated: return redirect(url_for('index')) #若登录,重定向到主页 # 若没有登陆 form = LoginForm() if form.validate_on_submit(): # 通过数据库验证用户身份 u = User.query.filter_by(username=form.username.data).first() if u is None or not u.check_password(form.password.data): # 如果验证不通过 print('Invalid username or password') return redirect(url_for('login')) # log user in login_user(u, remember=form.remember_me.data) # 设置登陆后的重定向页 next_page = request.args.get('next') if next_page: return redirect(next_page) return redirect(url_for('index')) return render_template('login.html', title="Login", form=form)