- 显示所有评论
{% for foo in ques.comments %} - 所有评论排序
uquestion = db.relationship(‘Question‘, backref=db.backref(‘comments‘, order_by=creat_time.desc)) - 显示评论条数
{{ ques.comments|length }} - 完成个人中心
1.个人中心的页面布局(html文件及相应的样式文件)
2.定义视图函数def usercenter(user_id):
3.向前端页面传递参数
4.页面显示相应数据
发布的全部问答
发布的全部评论
个人信息
5.各个页面链接到个人中心
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‘)) creat_time = db.Column(db.DateTime, default=datetime.now) detail=db.Column(db.Text,nullable=False) question=db.relationship(‘Question‘,backref=db.backref(‘comments‘,order_by=creat_time.deac)) author=db.relationship(‘User‘,backref=db.backref(‘comments‘)) #db.create_all() @app.route(‘/‘) def base(): return render_template(‘base.html‘) @app.route(‘/index/‘) def index(): context = { ‘question‘:Question.query.all() } return render_template(‘index.html‘,**context) @app.route(‘/detail/<question_id>‘) def detail(question_id): quest = Question.query.filter(Question.id == question_id).first() return render_template(‘detail.html‘,ques = quest) @app.route(‘/comment/‘,methods=[‘POST‘]) def comment(): comment = request.form.get(‘new_comment‘) 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) db.session.add(comm) db.session.commit() return redirect(url_for(‘detail‘,question_id=ques_id)) @app.route(‘/usercenter/<user_id>‘) def usercenter(user_id): user = User.query.filter(User.id == user_id).first() context = { ‘username‘: user.username, ‘wenda‘: user.wenda, ‘comments‘: user.comment } return render_template(‘usercenter.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.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(‘/question/‘,methods=[‘GET‘,‘POST‘]) @loginFirst #需要登录装饰器 def question(): if request.method==‘GET‘: return render_template(‘question.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.context_processor def mycontext(): usern=session.get(‘user‘) if usern: return{‘username‘:usern} else: return{} if __name__ == ‘__main__‘: app.run(debug=True)
{% extends ‘base.html‘ %} {% block title %}问答详情{% endblock %} {% block main %} <div class="page-header"> <h3>{{ ques.title }}<br><small>{{ ques.auther.username }} <span class="badge">{{ ques.creat_time }}</span></small></h3> </div> <p class="lead">{{ ques.detail }}</p> <hr> <form action="{{ url_for(‘comment‘) }}"method="post"style="..."> <div class="page-gronp"> <textarea name="new_comment"class="form-control"rows="3"id="new-comment"placeholder="write your comment"></textarea> <input name="question_id" type="hidden" value="{{ ques.id }}"/> </div> <button type="submit" class="btn bth-defauit">发送</button> </form> <h4>评论:({{ ques.comments|length }})</h4> <ul class="" style="width:58rem;margin-left: 4%;height: auto;margin-top: 0.5% "> {% for foo in ques.comment %} <li class="list-group-item"> <span class="glyphicon glyphicon-heart-empty"aria-hidden="true"></span> <a href="{{ url_for(‘usercenter‘,user_id = foo.auther.id) }}" >{{ foo.author.username }}</a> <span class="badge">{{ foo.creat_time }}</span> <p style="...">{{ foo.detail }}</p> </li> {% endfor %} </ul> {% endblock %}
{% extends ‘base,html‘ %} {% block title %}个人中心{% endblock %} {% block main %} <div class="page-header"> <h3><span class="glyphicon glyphicon-user"aria-hidden="true"></span>{{ user }}<br><small>全部问答<span class="badge"></span></small></h3> <ul class="list-group" style="..."> {% for foo in user-questions %} <li class="list-group-item"> <span class="glyphicon glyphicon-heart-empty"aria-hidden="true"></span> <a href="#" >{{ foo.author.username }}</a> <span class="badge">{{ foo.creat_time }}</span> <p style="...">{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> <div class="page-header"> <h3><span class="glyphicon glyphicon-user"aria-hidden="true"></span>{{ user }}<br><small>全部评论<span class="badge"></span></small></h3> <ul class="list-group" style="..."> {% for foo in user-commments %} <li class="list-group-item"> <span class="glyphicon glyphicon-heart-empty"aria-hidden="true"></span> <a href="#" >{{ foo.author.username }}</a> <span class="badge">{{ foo.creat_time }}</span> <p style="...">{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> <div class="page-header"> <h3><span class="glyphicon glyphicon-user"aria-hidden="true"></span>{{ user }}<br><small>个人信息<span class="badge"></span></small></h3> <ul class="list-group" style="..."> <li class="list-group-item">用户:{{ username }}</li> <li class="list-group-item">编号:</li> <li class="list-group-item">昵称:</li> <li class="list-group-item">文章篇数:</li> </ul> </div> {% endblock %}