登录功能完成:
1.js:设置return
function fnLogin() {
var oUname=document.getElementById("uname");
var oUpass=document.getElementById("upass");
var oError=document.getElementById("error_box");
var isError=true;
oError.innerHTML="
"
//uname
if ((oUname.value.length < 6) || (oUname.value.length > 20)) {
oError.innerHTML = "用户名要6-20位";
isError=false;
return isError;
}else if ((oUname.value.charCodeAt(0)>=48)&&(oUname.value.charCodeAt(0)<=57)){
oError.innerHTML="首字母必须是字母";
isError=false;
return isError;
}else {
for(var i=0;i
if ((oUname.value.charCodeAt(i)<48)||(oUname.value.charCodeAt(i)>57)&&(oUname.value.charCodeAt(i)<97) ||(oUname.value.charCodeAt(i)>122)){
oError.innerHTML="用户名只能是字母和数字";
isNotError=false;
return isError;
}
}
}
//upass
if ((oUpass.value.length < 6 )|| (oUpass.value.length > 20)) {
oError.innerHTML = "密码要6-20位";
isError=false;
return isError;
}
return isError;
window.alert("登录成功!")
}
2.html:设置
form
input{% extends'base.html' %}
{% block title %}登录{% endblock %}
{% block head %}
{% endblock %}
{% block main %}
登录
没有账号?立即注册
登录
{% endblock %}
3.py:
@app.route设置methods
GET
POST读取表单数据
查询数据库用户名密码对:记住用户名
跳转到首页
用户名密码不对:提示相应错误。from flask import Flask, render_template, request, redirect, url_for, session
from flask_sqlalchemy import SQLAlchemy
import config
app=Flask(__name__)
app.config.from_object(config)
db=SQLAlchemy(app)
# 创建用户模型
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
username = db.Column(db.String(20),nullable=False)
password = db.Column(db.String(20),nullable=False)
nickname=db.Column(db.String(50))
# db.create_all()
# 首页
@app.route('/')
def index():
return render_template('base.html')
#登录
@app.route('/login/',methods={'GET','POST'})
def login():
if request.method=='GET':
return render_template('login.html')
else:
username = request.form.get('username') # 获取form中的数据
password = request.form.get('password')
nickname = request.form.get('nickname')
user = User.query.filter(User.username == username).first()
if user:
if user.password==password:
session['user'] = username
session.permanent=True
return redirect(url_for('index')) # 返回到首页
else:
return u'error username or password' #输出错误
#注册
@app.route('/enroll/',methods={'GET','POST'})
def enroll():
if request.method=='GET':
return render_template('enroll.html')
else:
username=request.form.get('username')# 获取form中的数据
password=request.form.get('password')
nickname=request.form.get('nickname')
user = User.query.filter(User.username == username).first()
if user:
return u'username existed'
else:
user = User(username=username, password=password, nickname=nickname)
db.session.add(user)#数据库操作
mit()
return redirect(url_for('login'))# 重定向到登录页
#提问
@app.route('/questions/')
def questions():
return render_template('questions.html')
if __name__ == '__main__':
app.run(debug=True)
session:
从`flask`中导入`session`
设置`SECRET_KEY`
操作字典一样操作`session`:增加用户名`session['username']=`usernameimport os
SECRET_KEY = os.urandom(24)
SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@127.0.0.1:3306/mis_db?charset=utf8'
SQLALCHEMY_TRACK_MODIFICATIONS = False
1.网页运行结果:
成功运行跳转到首页:
2.数据库运行结果:
来源:/iamzhuangyuan/p/7871801.html