1.我的中心的頁面佈局(html文件及相應的樣式文件)css
2.定義視圖函數def usercenter(user_id):html
3.向前端頁面傳遞參數前端
4.頁面顯示相應數據sql
發佈的所有問答數據庫
發佈的所有評論flask
我的信息session
5.各個頁面連接到我的中心app
py函數
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): __table__name = '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)) 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')) author=db.relationship('User',backref=db.backref('question')) 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')) # 外鍵 detail = db.Column(db.Text, nullable=False) creat_time = db.Column(db.DateTime, default=datetime.now) question = db.relationship('Question', backref=db.backref('comments',order_by=creat_time.desc)) author = db.relationship('User', backref=db.backref('comments')) db.create_all() @app.route('/') def index(): context = { 'question': Question.query.all() } # question : Question.query.all() return render_template('index.html', **context) @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('/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.password==password: session['user']=username return redirect(url_for('index')) else: return u'password error' else: return u'username is not existed' @app.context_processor def mycontext(): usern=session.get('user') if usern: return{'username':usern} else: return{} @app.route('/logout/') def logout(): session.clear() return redirect(url_for('index')) def loginFirst(func): @wraps(func) def wrapper(*args,**kwargs): if session.get('user'): return func(*args,**kwargs) else: return redirect(url_for('login')) return wrapper @app.route('/lable/',methods=['GET','POST']) @loginFirst def lable(): if request.method == 'GET': return render_template('lable.html') else: title = request.form.get('title') detail = request.form.get('detail') author_id = User.query.filter(User.username == session.get('user')).first().id question = Question(title=title, detail=detail, author_id=author_id) db.session.add(question) db.session.commit() return redirect(url_for('index')) @app.route('/detail/<question_id>') def detail(question_id): # context={ # 'comments':Comment.query.all() # } quest = Question.query.filter(Question.id == question_id).first() return render_template('detail.html',ques=quest) @app.route('/comment/', methods=['POST']) @loginFirst def comment(): comment= request.form.get('commentdetail') ques_id=request.form.get('question_id') auth_id=User.query.filter(User.username==session.get('user')).first().id comm=Comment(author_id=auth_id,question_id=ques_id,detail=comment) # question.author=User db.session.add(comm) # 數據庫,添加操做 db.session.commit() #提交 return redirect(url_for('detail', question_id=ques_id)) @app.route('/usercenter/<user_id>') @loginFirst def usercenter(user_id): user=User.query.filter(User.id==user_id).first() context={ 'username':user.username, 'questions':user.question, 'comments':user.comments } return render_template('usercenter.html', ques=user) if __name__ == '__main__': app.run(debug=True)
usercenter.html佈局
{% extends 'base.html' %} {% block title %}我的中心{% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/usercenter.css') }}"> {% endblock %} {% block main %} <div class="a1"> <h3>{{ username }}<br> <small>所有問答</small> </h3></div> <ul> {% for foo in ques.question %} <li class="list-group-item" > <a href="#">{{ foo.author.username }} </a><br> <a href={{ url_for('detail',question_id=foo_id)}}>{{ foo.title }}</a> <span class="badge">{{ foo.creat_time }}</span> <p class="abstract">{{ foo.detail }}</p> </li> {% endfor %} <div class="a1"> <h3>{{ username }}<br> <small>所有評論</small> </h3></div> <ul> {% for foo in ques.comments %} <li class="list-group-item" > <a href="#">{{ foo.author.username }} </a><br> <a href={{ url_for('detail',question_id=foo_id)}}>{{ foo.title }}</a> <span class="badge">{{ foo.creat_time }}</span> <p class="abstract">{{ foo.detail }}</p> </li> {% endfor %} <div class="a1"> <h3>{{ username}}<br> <small>我的信息</small> </h3></div> <ul> <li>用戶:{{ username}}</li> <li>文章篇數:</li> </ul> {% endblock %}