1、系統概要說明html
一、設計數據庫,數據庫設計是網站設計的重點,須要對網站需求進行分析,設計適合網站需求的數據庫。數據庫
二、註冊頁面,設計網站須要用戶的使用,註冊則是重要的一部分,用戶註冊須要對用戶信息的管理,所以,須要數據庫的鏈接,進行用戶註冊的驗證。安全
三、登陸功能,在用戶註冊了帳號後,擁有了該網站的使用權,則能夠進行登陸,而登陸也須要數據庫的驗證。session
四、用戶發佈問答,該網站是博客的發佈平臺,用戶能夠將身邊的見聞發佈到網站,此功能也須要數據庫的鏈接,將用戶發表的問答存到數據庫的question表中,以用戶惟一的id號做爲外鍵。app
五、用戶發佈博客的問答後則會跳到網站首頁,首頁是展現該網站的因此博客信息,以發佈的最新日期排序進行展現,用戶可根據興趣點擊查看網站的博客標題,進入博客的詳情頁面,瞭解博客的具體信息。數據庫設計
六、博客詳情頁中具備評論及推薦文章功能,用戶可對博客進行評論發表還可對文章進行點贊及收藏,此操做過程存入數據庫的comment表中,以用戶id做爲外鍵;網站還根據用戶感興趣的博客推薦相應類型的文章。網站
七、我的中心頁面,網站根據用戶的活躍度,呈現用戶發佈的新聞,評論文章,收藏文章以及點贊。加密
八、網站具備模糊搜索功能,當用戶查找相應信息是,可根據用戶輸入的信息進行模糊搜索,提升用戶的體驗度。url
九、密碼保護功能,用戶註冊時設置的密碼存儲到數據庫中,此時數據會將用戶的密碼進行加密,提升用戶使用的安全性。設計
2、網站結構設計
1.登陸註冊
2.修改我的信息
3.搜索
4.發佈博客和評論
三.模塊詳細設計
1.首頁模塊
@app.route('/')
def index():
if request.args.get('info'):
info = request.args.get('info')
else:
info = None;
context = {
'questions': Question.query.order_by('-creat_time').all(),
'cf': Cf.query.all(),
'info': info,
'hot': Question.query.order_by('-click').all()[0:5]
} return render_template('index.html', **context)
2.註冊模塊
@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')
user = User.query.filter(User.username == username).first()
if user:
return 'error:user exitst'
else:
user = User(username=username, password=password)
db.session.add(user) # 加入數據庫
db.session.commit()
return redirect(url_for('login'))
3.登陸模塊
@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')
user = User.query.filter(User.username == username).first()
if user:
if user.check_password(password):
session['username'] = user.username
session['user_id'] = user.id
session.permanent = True
# 從新定位到首頁
return redirect(url_for('index'))
else:
# 從新定位到註冊
return redirect(url_for('login'))
else:
return redirect(url_for('login'))
4.發佈模塊
@app.route('/question', methods=['GET', 'POST'])
@loginFirst
def question():
if request.method == 'GET':
cf = Cf.query.all()
return render_template('question.html', cf=cf)
else:
title = request.form.get('title')
detail = request.form.get('detail')
author_id = request.form.get('author_id')
cf = request.form.get('cf')
question = Question(title=title, detail=detail, author_id=author_id, cf=cf)
db.session.add(question) # 加入數據庫
db.session.commit()
return redirect(url_for('index'))
5.評論模塊
@app.route('/answer/', methods=['GET', 'POST'])
def answer():
if request.method == 'POST':
question_id = request.form.get('question_id')
author_id = request.form.get('author_id')
detail = request.form.get('detail')
comment = Comment(question_id=question_id, author_id=author_id, detail=detail)
db.session.add(comment)
db.session.commit()
return redirect(url_for('detail', question_id=question_id))
@app.route('/commentaries/<user_id>')
def commentaries(user_id):
userCommentaries = Comment.query.filter(Comment.answer_id == user_id).all()
num = len(userCommentaries)
id = session.get('user_id')
if id:
user = User.query.filter(User.id == id).first()
else:
user = {}
return render_template('commentaries.html', userCommentaries=userCommentaries, user=user, num=num)
6.更改我的信息
@app.route('/uploadLogo/<user_id>', methods=['GET', 'POST'])
def uploadLogo(user_id):
user = User.query.filter(User.id == user_id).first()
f = request.files['logo']
basepath = os.path.dirname(__file__) # 當前文件所在路徑
upload_path = os.path.join(basepath, 'static/uploads', f.filename) # 注意:沒有的文件夾必定要先建立,否則會提示沒有該路徑
f.save(upload_path)
user.icon = 'uploads/' + f.filename
db.session.commit()
return redirect(url_for('setPassword', id=user_id));
@app.route('/setPassword/<id>', methods=['GET', 'POST'])
@loginFirst
def setPassword(id):
if request.method == 'GET':
return render_template('setPassword.html')
else:
user = User.query.filter(User.id == id).first()
if user:
if user.check_password(request.form.get('old')):
user.password = request.form.get('new1')
db.session.commit()
info = '修改爲功'
else:
info = '原密碼錯誤'
else:
info = '未知錯誤'
return redirect(url_for('index', info=info))
7.模糊查詢
@app.route('/search')
def search():
qu = request.args.get('q')
c = '' if request.args.get('c') == '' else request.args.get('c')
y = '' if request.args.get('y') == '' else request.args.get('y')
query = Question.query.filter(
or_(
Question.title.contains(qu),
Question.detail.contains(qu),
),
Question.cf.like('%' + c + '%'),
Question.creat_time.like('%' + y + '%'),
).order_by('-creat_time').all()
context = {
'questions': query,
'cf': Cf.query.all(),
'hot': Question.query.order_by('-click').all()[0:5]
}
return render_template('index.html', **context)
四.數據庫設計
Collection = db.Table(
'collection',
db.Column('id', db.Integer, primary_key=True, autoincrement=True),
db.Column('book_id', db.Integer, db.ForeignKey('question.id')), # 評論對應的文章的id
db.Column('collection', db.Integer, db.ForeignKey('user.id')), # 收藏用戶的id
db.Column('createdate', db.DATETIME) # 發佈時間
)
class User(db.Model):
__tablename__ = 'user'
# 創建一個表user
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(20), nullable=False)
_password = db.Column(db.String(200), nullable=False)
say = db.Column(db.String(50))
icon = db.Column(db.String(50))
collection = db.relationship('Question', secondary=Collection, backref=db.backref('user', lazy='dynamic'),
lazy='dynamic')
@property
def password(self):
return self._password
@password.setter
def password(self, row_password):
self._password = generate_password_hash(row_password)
def check_password(self, row_password):
return check_password_hash(self._password, row_password)
class Comment(db.Model):
__tablename__ = 'comment'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
question_id = db.Column(db.Integer, db.ForeignKey('question.id'))
creat_time = db.Column(db.DateTime, default=datetime.now())
detail = db.Column(db.TEXT, nullable=False)
question = db.relationship('Question', backref=db.backref('comment'))
author = db.relationship('User', backref=db.backref('comment', order_by=creat_time.desc))
class Question(db.Model):
__tablename__ = 'question'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(100), nullable=False)
detail = db.Column(db.Text, nullable=False)
creat_time = db.Column(db.DateTime, default=datetime.now)
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
cf = db.Column(db.Integer, db.ForeignKey('cf.id'))
look = db.Column(db.Integer)
click = db.Column(db.Integer)
author = db.relationship('User', backref=db.backref('question'))
cfClass = db.relationship('Cf', backref=db.backref('question'))
六.成品展現