- js文件: onclick函数return True时才提交表单,return False时不提交表单。
- html文件:
- <form>中设置 action和method="post"
- <input> 中设置 name
- 主py文件中:
- from flask import request, redirect, url_for
- @app.route('/regist/', methods=['GET', 'POST’])
def regist():
if request.method == 'GET':
return render_template('regist.html')
else:
username = request.form.get(‘username’)#获取form中的数据
判断用户名是否存在
存到数据库中
redirect重定向到登录页
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
function fnRefist() {
var oUname
=
document.getElementById(
"uname"
)
var oError
=
document.getElementById(
"error_box"
)
var oUpass
=
document.getElementById(
"upass"
)
var oUpass1
=
document.getElementById(
"upass1"
)
oError.innerHTML
=
"<br>"
if
(oUname.value.length <
6
|| oUname.value.length >
20
) {
oError.innerHTML
=
"用户名必须在6-20之间"
;
return
;
}
else
if
((oUname.value.charCodeAt(
0
) >
=
48
) && (oUname.value.charCodeAt(
0
) <
=
57
)) {
oError.innerHTML
=
"首字母不能为数字"
;
return
;
}
else
for
(var i
=
0
; i < oUname.value.length; i
+
+
) {
if
((oUname.value.charCodeAt(i) <
48
) || (oUname.value.charCodeAt(i) >
57
) && ((oUname.value.charCodeAt(i) <
97
)) || oUname.value.charCodeAt(i) >
122
) {
oError.innerHTML
=
"只能填写数字或字母"
;
return
;
}
}
/
/
oUpass
if
(oUpass.value.length >
20
|| oUpass.value.length <
6
) {
oError.innerHTML
=
"密码必须在6-20之间"
;
return
;
}
else
if
(oUpass.value!
=
oUpass1.value) {
document.getElementById(
"error_box"
).innerHTML
=
"两次密码不一致"
return
false;
}
return
true;
window.alert(
"注册成功!"
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
{
%
extends
'index.html'
%
}
{
%
block title
%
}注册 {
%
endblock
%
}
{
%
block head
%
}
<link rel
=
"stylesheet"
type
=
"text/css"
href
=
"{{ url_for('static',filename='css/886.css') }}"
>
<script src
=
"{{ url_for('static',filename='js/00.js')}}"
><
/
script>
{
%
endblock
%
}
{
%
block main
%
}
<div
class
=
"box"
>
<h2
class
=
"dd"
>注册<
/
h2>
<form action
=
"{{ url_for('regist') }}"
method
=
"post"
>
<div
class
=
"input_box"
>
<
input
id
=
"uname"
type
=
"text"
placeholder
=
"请输入用户名"
name
=
"username"
>
<br><
input
id
=
"unickname"
type
=
"text"
placeholder
=
"请输入昵称"
name
=
"nickname"
>
<
/
div>
<div
class
=
"input_box"
>
<br><
input
id
=
"upass"
type
=
"password"
placeholder
=
"请输入密码"
name
=
"password"
>
<br><
input
id
=
"upass1"
type
=
"password"
placeholder
=
"请确认密码"
>
<
/
div>
<br><div
id
=
"error_box"
><br><
/
div><br>
<div>
<br>
<br><button
class
=
"jj"
onclick
=
"fnRefist()"
>注册<
/
button>
<
/
div>
<
/
form>
<
/
div>
{
%
endblock
%
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
from
flask
import
Flask,render_template,request,redirect,url_for,session
from
flask_sqlalchemy
import
SQLAlchemy
import
config
from
functools
import
wraps
from
datetime
import
datetime
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()
# 增加
# user = User(username='mis1114',password = 'mis1114',nickname='mis1114')
# db.session.add(user)
# db.session.commit()
# # 查询
# User.query.filter(User.username == 'mis1114').first()
# #修改
# user =User.query.filter(User.username =='mis1114').first()
# user.password='23456'
# print(user.username,user.password)
# db.session.commit()
# # 删除
# user =User.query.filter(User.username =='mis1114').first()
# db.session.delete(user)
# db.session.commit()
@app
.route(
'/'
)
def
index():
return
render_template(
'index.html'
)
@app
.route(
'/login/'
)
def
login():
return
render_template(
'login.html'
)
@app
.route(
'/regist/'
,methods
=
[
'GET'
,
'POST'
])
def
regist():
if
request.method
=
=
'GET'
:
return
render_template(
'regist.html'
)
else
:
username
=
request.form.get(
'username'
)
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)
db.session.commit()
return
redirect(url_for(
'login'
))
@app
.route(
'/question/'
)
def
question():
return
render_template(
'question.html'
)
if
__name__
=
=
'__main__'
:
app.run(debug
=
True
)
|