完成评论功能
Posted 008马佳槟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成评论功能相关的知识,希望对你有一定的参考价值。
from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy from functools import wraps from datetime import datetime import config,os app=Flask(__name__) app.config.from_object(config) db=SQLAlchemy(app) class User(db.Model): __tablename__=‘user‘ id=db.Column(db.Integer,primary_key=TabError,autoincrement=True) username=db.Column(db.String(20),nullable=False) password=db.Column(db.String(20),nullable=False) nickname = db.Column(db.String(20), nullable=True) class Question(db.Model): __tablename__=‘wenda‘ id=db.Column(db.Integer,primary_key=TabError,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(‘wenda‘)) class Comment(db.Model): __tablename__=‘comment‘ id=db.Column(db.Integer,primary_key=TabError,autoincrement=True) author_id=db.Column(db.Integer,db.ForeignKey(‘user.id‘)) question_id=db.Column(db.Integer,db.ForeignKey(‘wenda.id‘)) detail=db.Column(db.Text,nullable=False) creat_time=db.Column(db.DateTime,default=datetime.now) author = db.relationship(‘User‘,backref=db.backref(‘comment‘)) question=db.relationship(‘Question‘,backref=db.backref(‘comment‘)) db.create_all() #是否链接成功 # 数据添加 user=User(username=‘ma‘,password=‘gg‘) db.session.add(user) db.session.commit() # # 数据更新 # # user = User.query.filter(User.username==‘hh‘).first() # user.password=‘gg‘ # db.session.commit() # # # 数据查询 # user = User.query.filter(User.username==‘ma‘).first() # print(user.username,user.password) # # 数据删除 # user = User.query.filter(User.username==‘ma‘).first() # db.session.delete(user) # db.session.commit() # session会话连接 # filter()过滤器 # route制定路径和函数之间的关系 # def定义一个变量 @app.route(‘/‘) def switch(): context = { ‘wenda‘:Question.query.all() } return render_template(‘switch.html‘,**context) @app.route(‘/login/‘,methods=[‘GET‘,‘POST‘])# 跳转登陆,methods定义它有两种请求方式 def login(): if request.method == ‘GET‘: return render_template(‘login.html‘) else: username = request.form.get(‘user‘) password = request.form.get(‘psw‘) user = User.query.filter(User.username == username).first() # 判断用户名是否存在 if user: if user.password==password: session[‘user‘] = username session.permanent = True return redirect(url_for(‘switch‘)) else: return u‘密码错误‘ else: return u‘用户名不存在‘ @app.context_processor def mycontext(): username=session.get(‘user‘) if username: return {‘username‘:username} else: return{} @app.route(‘/logout‘) def logout(): session.clear() return redirect(url_for(‘switch‘)) @app.route(‘/register/‘,methods=[‘GET‘,‘POST‘])# 跳转注册,methods定义它有两种请求方式 def register(): if request.method == ‘GET‘: return render_template(‘register.html‘) else: username = request.form.get(‘user‘) password = request.form.get(‘psw‘) nickname = request.form.get(‘nickname‘) user = User.query.filter(User.username == username).first() # 判断用户名是否存在 if user: return u‘该用户已存在‘ else: user = User(username=username,password=password,nickname=nickname) db.session.add(user) db.session.commit() return redirect(url_for(‘login‘)) # 进入问答界面先登录功能 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(‘/wenda/‘,methods=[‘GET‘,‘POST‘]) @loginFirst def wenda(): if request.method==‘GET‘: return render_template(‘wenda.html‘) else: title=request.form.get(‘title‘) detail=request.form.get(‘detail‘) author_id = User.query.filter(User.username == session.get(‘user‘)).first().id wenda = Question(title=title,detail=detail,author_id=author_id) db.session.add(wenda) db.session.commit() return redirect(url_for(‘switch‘)) @app.route(‘/detail/<question_id>‘) def detail(question_id): quest=Question.query.filter(Question.id==question_id).first() return render_template(‘detail.html‘,quest=quest) @app.route(‘/comment/‘,methods=[‘POST‘]) def comment(): comment=request.form.get(‘new_comment‘) question_id=request.form.get(‘question_id‘) author_id=User.query.filter(User.username==session.get(‘user‘)).first().id comm=Comment(detail=comment,question_id=question_id,author_id=author_id) db.session.add(comm) db.session.commit() return redirect(url_for(‘detail‘,question_id=question_id)) if __name__ == ‘__main__‘: app.run(debug=True)
以上是关于完成评论功能的主要内容,如果未能解决你的问题,请参考以下文章