好久更新博客里,今天写完项目,总结一下,当作备忘录,真的真的不想在写后台了,太折腾了:happy:,这次项目,总的来说还是挺充实的,采用了以前的框架,写起来还是很快的。进度也还不错,到今天一共五天,虽然说遇到了n多的坑,但总算是完成了,累。
整体框架
这次项目整体的框架还是用的上一个项目的蓝图框架,认证方式采用的是JWT,同时继承了cookie认证,可以在不同场合使用。比如说微信小程序就比较适合用JWT认证。最近和队友一起在做小程序,感觉还是很好的,前端时间因为临时做这个项目,耽搁了不少时间,现在要赶一下进度了。
踩坑总结
解决sa_instance_state报错
AttributeError: 'Manage' object has no attribute '_sa_instance_state'
这个错误发生在user/comment路由里面,想了很久,也问过学长,最后都没找到原因,但还是暴力调试解决了问题,代码如下。
class Message(db.Model): __tablename__ = 'message' __table_args__ = {'mysql_charset': 'utf8'} id = db.Column(db.Integer,primary_key=True,autoincrement=True) message_content = db.Column(db.Text,nullable=False) message_from_name = db.Column(db.String(30),nullable=False) message_data = db.Column(db.DateTime,default = datetime.datetime.utcnow()) message_user_id = db.Column(db.Integer , db.ForeignKey('user.id')) def __init__(self , *args, **kwargs): super().__init__() if 'message_content' in kwargs: self.message_content = kwargs['message_content'] if 'message_from_name' in kwargs: self.message_from_name = kwargs['message_from_name'] if 'message_user_full' in kwargs: self.user = kwargs['message_user_full'] def get_message(self): dic={'message_content':self.message_content ,'message_from_name':self.message_from_name ,'message_data':self.message_data} return dic def to_json(self): return {'message_content':self.message_content,'message_from_name':self.message_from_name}
#views.py_v1部分代码
@main.route("/course/comment",methods=['POST','GET'])
@jwt_required()
def course_comment():
user = User.query.filter_by(id=current_identity.id).first()
dic = json_loads()
if 'comment_body' in dic and 'comment_course_id' in dic:
course = Course.query.filter_by(id=dic['comment_course_id']).first()
c = {}
t = 0
for i in course.comment.order_by('-comment_date').all():
c.update(
{t: {'comment_user_name': i.user.name, 'comment_body': i.comment_body, 'comment_date': i.comment_date}})
t += 1
if 'is_comment_on_user' in dic:
if bool(dic['is_comment_on_user']) and 'comment_on_user_id' in dic:
user_tmp=User.query.filter_by(id=dic['comment_on_user_id']).first()
str1 = user.name+"评论了您在"+course.course_name+"课程的评论"
mcc_print(str1)
user_tmp.message.append(Message(message_content=str1,message_from_name=user.name,message_user_full=user))
c_tmp=Comment(**dic)
user.comment.append(c_tmp)
c.update(
{t: {'comment_user_name': user.name, 'comment_body': dic['comment_body'],
'comment_date': c_tmp.comment_date}})
db.session.add(user_tmp)
db.session.commit()
else:
user.comment.append(Comment(**dic))
else:
user.comment.append(Comment(**dic))
db.session.add(user)
db.session.commit()
return jsonify({'StatusCode': 200, 'info': '评论用户成功', 'comment_all': c})
else:
return jsonify({'StatusCode':400,'info':'请求未填写完整'})
#views.py_v2部分代码
@main.route("/course/comment",methods=['POST','GET'])
@jwt_required()
def course_comment():
user = User.query.filter_by(id=current_identity.id).first()
dic = json_loads()
if 'comment_body' in dic and 'comment_course_id' in dic:
course = Course.query.filter_by(id=dic['comment_course_id']).first()
大专栏 AXX项目总结c = {}
t = 0
for i in course.comment.order_by('-comment_date').all():
c.update(
{t: {'comment_user_name': i.user.name, 'comment_body': i.comment_body, 'comment_date': i.comment_date}})
t += 1
if 'is_comment_on_user' in dic:
if bool(dic['is_comment_on_user']) and 'comment_on_user_id' in dic:
user_tmp=User.query.filter_by(id=dic['comment_on_user_id']).first()
str1 = user.name+"评论了您在"+course.course_name+"课程的评论"
mcc_print(str1)
message=Message()
message.message_content=str1
message.message_from_name=user.name
message.user=user_tmp
c_tmp=Comment(**dic)
user.comment.append(c_tmp)
c.update(
{t: {'comment_user_name': user.name, 'comment_body': dic['comment_body'],
'comment_date': c_tmp.comment_date}})
db.session.add(user_tmp)
db.session.commit()
else:
user.comment.append(Comment(**dic))
else:
user.comment.append(Comment(**dic))
db.session.add(user)
db.session.commit()
return jsonify({'StatusCode': 200, 'info': '评论用户成功', 'comment_all': c})
else:
return jsonify({'StatusCode':400,'info':'请求未填写完整'})
详细的原因也不是很清楚,但最后还是解决了
- db.sesseion.commit()时报错
#具体的报错信息因为忘记保存就不贴了,但遇到这中文题一定要检查数据库的数据要求与你提交的数据库的要求之间进行对比,比如说nullable=False的列就不能缺,否者会commit提交不了,已经数据的类型也要注意,前后端交互,通过表单传递的一般是string,因此检查前端传来的参数十分必要
- db.sesseion.add()报错
#当时报这个错真的有点崩,完全没遇到过,要说传参出错,那也是应该在commit那一步报错,但add这一步就报错,很奇怪,最后解决方法,在query.filter_by(search_information=kwargs['search_information'])后加上first(),虽然说知道这个用法,但是,哎,可能还是老了。
def db_user_push(**kwargs):
if 'name' in kwargs:
user = User.query.filter_by(name=kwargs['name']).first()
if current_user.is_authenticated:
user = current_user._get_current_object()
if current_identity:
user = User.query.filter_by(id=current_identity.id).first()
else:
user=User(**kwargs)
if 'name' in kwargs:
user.name=kwargs['name']
if 'password' in kwargs:
user.password=generate_password_hash(kwargs['password'])
if 'confirmed' in kwargs:
user.confirmed = bool(kwargs['confirmed'])
if 'email' in kwargs:
user.email = kwargs['email']
if 'icon' in kwargs:
user.icon=kwargs['icon']
if 'search_information' in kwargs:
tmp=Search_information.query.filter_by(search_information=kwargs['search_information']).first()
if tmp:
tmp.search_time+=1
if tmp not in user.search_information:
user.search_information.append(Search_information(**kwargs))
else:
user.search_information.append(Search_information(**kwargs))
if 'comment_body' in kwargs and 'comment_course_id' in kwargs and 'comment_on_user_id' in kwargs:
user.comment.append(Comment(**kwargs))
if 'message_content' in kwargs and 'message_from_user_name' in kwargs:
user.message.append(Message(message_content=kwargs['message_content'],message_from_name=kwargs['message_from_name']))
if 'role' in kwargs:
user.Role = Role.query.filter_by(role=kwargs['role']).first()
if 'course_name' in kwargs and 'course_name' in kwargs and 'course_score' in kwargs and 'course_target' in kwargs
and 'course_address' in kwargs and 'course_class_num' in kwargs and 'course_time_start' in kwargs
and 'course_time_end' in kwargs and 'course_attr' in kwargs and 'course_teacher_name' in kwargs
and 'course_check_type' in kwargs and 'course_time_week' in kwargs:
user.course.append(Course(**kwargs))
db.session.add(user)
db.session.commit()
return user
项目感受
这次的项目感受还是很深的,五天忙里偷闲的,感觉很充实,也决定了自己接下来的方向,很多时候就是这样,自己的抉择,选定了就踏踏实实的做,遇到的一切都不算什么,压力什么的谁都会有,全力迈过之后,会很是一次很好的回忆