1
from flask import flask, render_template, request, redirect, url_for, session
2from flask_sqlalchemy import sqlalchemy
3import config
4from functools import wraps
5from datetime import datetime67
891011
12class user(db.model):
13__tablename__ = 'user'
14id = db.column(db.integer, primary_key=true, autoincrement=true)
15username = db.column(db.string(20), nullable=false)
16password = db.column(db.string(20), nullable=false)
17nickname = db.column(db.string(20), nullable=true)
1819
class question(db.model):
20__tablename__ = 'question'
21id = db.column(db.integer, primary_key=true, autoincrement=true)
22author_id=db.column(db.integer,db.foreignkey('user.id'))
23title=db.column(db.string(100), nullable=false)
24detail=db.column(db.text, nullable=false)
25creat_time=db.column(db.datetime,default=datetime.now)
26author=db.relationship('user',backref=db.backref('question'))
2728
db.create_all() #是否鏈結成功
2930
31# 資料新增
32# user=user(username='ma',password='gg')
33# db.session.add(user)
34# db.session.commit()35#
36#37# 資料更新38#
39# user = user.query.filter(user.username=='hh').first()
40# user.password='gg'
41# db.session.commit()42#
43#44# 資料查詢
45# user = user.query.filter(user.username=='ma').first()
46# print(user.username,user.password)47#
48# 資料刪除
49# user = user.query.filter(user.username=='ma').first()
50# db.session.delete(user)
51# db.session.commit()
5253
54# session會話連線
55# filter()過濾器
56# route制定路徑和函式之間的關係
57# def定義乙個變數
5859
60def switch():
61return render_template('switch.html')
6263
6465
def login():
66if request.method == 'get':
67return render_template('login.html')
68else:
69username = request.form.get('user')
70password = request.form.get('psw')
71user = user.query.filter(user.username == username).first() # 判斷使用者名稱是否存在
72if user:
73if user.password == password:
74session['user']=username
75session.permanent = true
76return redirect(url_for('switch'))
77else:
78return '使用者名稱不存在'
7980
def mycontext():
81username=session.get('user')
82if username:
83return
84else:
85return {}
8687
def loginfirst(func):
88@wraps(func)
8990
if session.get('user'):
91return func(*args,**kwargs)
92else:
93return redirect(url_for('login'))
9495
9697
# @loginfirst
98# def question():
99# if request.method=='get':
100# return render_template('question.html')
101# else:
102# title=request.form.get('title')
103# detail=request.form.get('detail')
104# author_id=user.query.filter(user.username==session.get('user')).first().id
105# question=question(title= title,detail=detail,quthor_id=author_id)
106# db.session.add( question )
107# db.session.comit()
108# return (url_for('index'))
109110
111def register():
112if request.method == 'get':
113return render_template('register.html')
114else:
115116
username = request.form.get('user')
117password = request.form.get('psw')
118nickname = request.form.get('nickname')
119user = user.query.filter(user.username == username).first() # 判斷使用者名稱是否存在
120if user:
121return u'該使用者已存在'
122else:
123user = user(username=username, password=password, nickname=nickname)
124db.session.add(user)
125db.session.commit()
126return redirect(url_for('login'))
127128
129def logout():
130session.clear()
131return render_template('switch.html')
132133
134def wenda():
135return render_template('wenda.html')
136137
138if __name__ == '__main__':
完成註冊功能
js檔案 onclick函式 return true時才提交表單,return false時不提交表單。html檔案 中設定 action和method post 中設定 name 主py檔案中 from flask import request,redirect,url for def regis...
完成註冊功能
js檔案 onclick函式return true時才提交表單,return false時不提交表單。function fnlogin else if ouname.value.charcodeat 0 48 ouname.value.charcodeat 0 57 else for var i 0...
完成註冊功能
js檔案 onclick函式 return true時才提交表單,return false時不提交表單。html檔案 中設定 action和method post 中設定 name 主py檔案中 from flask import request,redirect,url for def regis...