def login(): form = LoginForm() if request.method == 'GET': return render_template('login.html', form=form) else: user_name = form.user_name.data user_password = form.user_password.data user = User.query.filter(User.userName == user_name).first() if user and user.verify_password(user_password): session['user_name'] = user.userName # session.permanent = True g.user_name = user_name g.user_ip = request.remote_addr # nginx 配置:proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # g.user_ip = request.headers['X-Forwarded-For'] g.user_login_time = datetime.now() manage.app.logger.info("login():current login user is " + g.user_name + " & login at " + str(g.user_login_time) + " & user ip:" + g.user_ip) login_log() # 更新用户上次登录时间 print(user) user.lastLoginTime = datetime.now() db.session.add(user) db.session.commit() return redirect(url_for('main.index')) else: flash(u'登录失败,账号或者密码不正确!!!') return redirect(url_for('center.login'))
def login(): if request.method == 'GET': return render_template('login.html') else: g.useername = request.form.get('username') g.password = request.form.get('password') if g.useername == 'z' and g.password == '1': # 默认当前这个用户的用户名和密码正确 login_log() return '恭喜登录成功!' else: return '你的用户名或密码错误!'
def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') if username == 'Zac' and password == '1234': g.username = '******' login_log() return '登录成功!' else: return '帐号密码错误!'
def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') if username == 'zhaomeng' and password == '1': # 就认为当前这个用户的用户名和密码正确 g.username = '******' g.ip = 'xx' login_log() return '恭喜,登录成功' else: return u'您的用户名和密码错误!'
def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') if username == 'admin' and password == 'password': # 认为该用户登录成功 # login_log(username) g.username = username login_log() return 'login success.' else: return 'username or password error.'
def login(): if request.method == 'GET': return render_template('index.html') else: username = request.form.get('username') password = request.form.get('password') if username == 'sunhaiyang' and password == '111111': #g对象用来绑定用户,在所有页面都可以使用 # login_log(username) g.username = '******' login_log() return u'登陆成功' else: return u'您的用户名或密码错误'
def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') #获取后台数据 password = request.form.get('password') if username == 'zhijin' and password == 'huashi': session['username'] = '******' g.username = '******' #g在同一次浏览器的请求中的所有代码均可以使用 g.password = '******' login_log() return u'恭喜!登录成功!' else: return u'你的用户名或者密码错误'
def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') if username == 'zhiliao' and password == '111111': #就认为用户名和密码正确 g.username = '******'#将'zhiliao'绑定到全局变量g.username当中去 g.ip = 'xxx.xxx.xxx.xxx'#将'xxx.xxx.xxx.xxx'绑定到全局变量g.ip当中去 login_log() login_ip_log() return u'登陆成功' else: return u'您的用户名或密码错误!'
def post_login(): if request.method == "POST": post_data = request.form print(post_data) username = post_data["username"] password = post_data.get("password") g.username = username login_log() g.ip = "192.168.0.1" login_ip_log() if username == "flask" and password == "password": return "Login successfully." else: return "Login failure." else: return render_template("post_login.html")
def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') if username == 'zhiliao' and password == '111': #g:是专门用来保存用户的数据的 #g对象在一次请求中的所有代码的地方,都是可以使用的. g.username = '******' g.ip = 'xx' login_log() print(username, password) return '登录成功' else: return '没有该用户'
def login(): if request.method == 'GET': return render_template('login.html') else: u = request.form.get('username') p = request.form.get('password') if u == 'chenxudong' and p == '123456': # 方法1:通过传参 # login_log(u) # 方法2:通过全局变量g对象 g.username = u g.password = p login_log() return '%s登陆成功!' % u else: return '您的用户名或密码错误!'
def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') print('username:%s' % username) print('password:%s' % password) if username == 'zhiliao' and password == '111111': # 满足这个条件,就默认这个用户登录成功 # # 此处调用不同函数要传许多不同的参数 # login_log(username) # 可以利用g对象,将要传的参数绑定在g对象中,然后在utils中导入g,调用函数时就不用传参数了 g.username = username g.ip = 'xxxxx' login_log() return '登录成功!' pass else: return '你的用户名或密码错误!'
from flask import Flask, g, render_template, request from utils import login_log app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello World!' @app.route('/login/', methods=['GET', 'POST']) def login(): if request.method == 'GET': return render_template('login.html') else: username = request.form.get('username') password = request.form.get('password') if username == 'Zac' and password == '1234': g.username = '******' login_log() return '登录成功!' else: return '帐号密码错误!' if __name__ == '__main__': app.run(debug=True) login_log()