博客设计
一、系統概要說明
1、設計數據庫,數據庫設計是網站設計的重點,需要對網站需求進行分析,設計適合網站需求的數據庫。
2、注冊頁面,設計網站需要用戶的使用,注冊則是重要的一部分,用戶注冊需要對用戶信息的管理,因此,需要數據庫的連接,進行用戶注冊的驗證。
3、登錄功能,在用戶注冊了賬號后,擁有了該網站的使用權,則可以進行登錄,而登錄也需要數據庫的驗證。
4、用戶發布問答,該網站是博客的發布平臺,用戶可以將身邊的見聞發布到網站,此功能也需要數據庫的連接,將用戶發表的問答存到數據庫的question表中,以用戶唯一的id號作為外鍵。
5、用戶發布博客的問答后則會跳到網站首頁,首頁是展示該網站的所以博客信息,以發布的最新日期排序進行展示,用戶可根據興趣點擊查看網站的博客標題,進入博客的詳情頁面,了解博客的具體信息。
6、博客詳情頁中具有評論及推薦文章功能,用戶可對博客進行評論發表還可對文章進行點贊及收藏,此操作過程存入數據庫的comment表中,以用戶id作為外鍵;網站還根據用戶感興趣的博客推薦相應類型的文章。
7、個人中心頁面,網站根據用戶的活躍度,呈現用戶發布的新聞,評論文章,收藏文章以及點贊。
8、網站具有模糊搜索功能,當用戶查找相應信息是,可根據用戶輸入的信息進行模糊搜索,提高用戶的體驗度。
9、密碼保護功能,用戶注冊時設置的密碼存儲到數據庫中,此時數據會將用戶的密碼進行加密,提高用戶使用的安全性。
?
二、網站結構設計
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'))六.成品展示
?
?
?
????
轉載于:https://www.cnblogs.com/z1-z/p/9191112.html
總結
- 上一篇: 关于一些blog优化
- 下一篇: GCC入门