个人学期总结
在本学期学习python语言的过程中,遇到过很多的问题和困难,所幸可以得到老师和同学们的帮助。时光像水中的倒影,一晃一学期就过去了。昨日那埋怨时间过的太慢的情素似乎还游移在脑际,而今大三下学期的生活正在向我们走来,蓦然回首,感慨颇多。刚迈入大学的时候对一切似乎都充满新鲜感,于是到处跃跃欲试,结果碰壁较多,吃一堑长一智,大一大二除了努力完成自己的学习目标也利用各种活动丰富自己的生活摆脱现在大学生最流行的郁闷日子。
刚开始学习管理信息系统的时候,以为这是一门很简单的科目,是偏文字的科目,平常靠看看背背就完事了,学起来才发现是完全不一样,从上课的第一天到现在,不知不觉已经发表了接近40篇的博客,一开始不太了解这门课的实际意义在哪里,包括在做作业的时候也不太了解,逐渐地,在系统一步步完善的同时,也开始发现这门课有什么奥妙,平常觉得别人的网页别人的网站很拉风很好
看,为什么可以自己做出导航、登录注册、个人中心等页面呢,直到自己也根据老师说的一步步做出来之后才发现,其实我自己也可以做得出来,从一开始的觉得完成基本作业,把功能实现出来到后来开始追求完美,无论样式、排版都要做好,这一个也是学习的过程。
一个学期说长不长,说短也不短,可能是在忙碌和充实中度过,所以觉得这个学期过得特别的快,也过得特别的有意义,学到了很多非计算机专业学不到的知识,也开始觉得其实互联网的奥妙是在于如何深入学习,一个学期以来,做了很多作业,在作业上遇到很多不一样的问题和难题,但是每次老师都会耐心解答,同时也感谢创客的同学教会我很多不一样的知识,学习计算机的道路很漫长,所以我知道,每个人都在努力。
学习过程:
一、使用软件
二、开始flask项目
from flask import Flask,render_templateapp=Flask(__name__)@app.route('/')def base(): return render_template('base.html')@app.route('/login/')def login(): return render_template('denglu.html')@app.route('/signin/')def zhuce(): return render_template('zhuce.html')if __name__=='__main__': app.run(debug=True)复制代码复制代码广州商学院 广州商学院
2017
每天新鲜事
登录
Username: Passward: student teacher 记住我 登录遇到的问题
三、夜间模式、父模板制作
夜间模式 ppppppp
四、加载静态文件,父模板的继承与扩展
{% block title %}{% endblock %}base { % block head %}{% endblock %}{ % block main %}{% endblock %}
五、连接数据库,创建用户类型
from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyimport configapp = Flask(__name__)app.config.from_object(config)db=SQLAlchemy(app)class User(db.Model): __tablename__ = '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(20))db.create_all()@app.route('/')def hello_world(): return 'Hello World!'if __name__ == '__main__': app.run()复制代码SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:@127.0.0.1:3306/test?charset=utf8'SQLALCHEMY_TRACK_MODIFICATIONS = False
六、完成注册功能
from flask import Flask,render_template,request,redirect,url_forfrom flask_sqlalchemy import SQLAlchemyimport configapp = Flask(__name__)app.config.from_object(config)db=SQLAlchemy(app)class User(db.Model): __tablename__ = '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(20))db.create_all()# user=User(username='mis1234',password='111111')# db.session.add(user)# db.session.commit()@app.route('/',methods=['GET','POST'])def register(): if request.method=='GET': return render_template('登录.html') else: username=request.form.get('username') passward=request.form.get('passward') nickname=request.form.get('nickname') user=User.query.filter(User.username==username).first() if user: return 'username existed' else: user=User(username=username,passward=passward,nickname=nickname) db.session.add(user) db.session.commit() return redirect(url_for('登录'))@app.route('/load',methods=['GET','POST'])def load(): if request.method == 'GET': return render_template('登录.html')@app.route('/')def base(): return render_template('base.html')@app.route('/login/')def login(): return render_template('登录.html')@app.route('/signin/')def signin(): return render_template('zhuce.html')@app.route('/fabu/')def fabu(): return render_template('发布.html')if __name__=='__main__': app.run(debug=True)if __name__ == '__main__': app.run()
七、完成登录功能
from flask import Flask,render_template,request,redirect,url_forfrom flask_sqlalchemy import SQLAlchemyimport configapp = Flask(__name__)app.config.from_object(config)db=SQLAlchemy(app)class User(db.Model): __tablename__ = '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(20))db.create_all()# user=User(username='mis1234',password='111111')# db.session.add(user)# db.session.commit()@app.route('/')def base(): return render_template("base.html")@app.route('/login/', methods=['GET', 'POST'])def login(): if request.method == 'GET': return render_template("denglu.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: return redirect(url_for('base')) else: return '密码错误' else: return '用户名不存在'@app.route('/regist/', methods=['GET', 'POST'])def zhuce(): if request.method == 'GET': return render_template("zhuce.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 '用户名已存在' else: user = User(username=username, password=password, nickname=nickname) db.session.add(user) # 数据库,添加操作 db.session.commit() return redirect(url_for('login'))@app.route('/fabu/')def fabu(): return render_template('question.html')if __name__=='__main__': app.run(debug=True)复制代码复制代码{ % extends'fumoban.html' %}登录 { % block head %} { % endblock %}{ % block main %}用户登录
八、登陆后更新导航
{% block title %}{% endblock %}base { % block head %}{% endblock %}{ % block main %}{% endblock %}{% block title %}{% endblock %}base { % block head %}{% endblock %}{ % block main %}{% endblock %}
九、发布功能完成
def fabu(): return render_template('question.html'){ % extends 'base.html' %}{ % block title %}问答{% endblock %}{ % block head %} { % endblock %}{ % block main %}{ % endblock %}
十、首页列表显示全部问答,完成问答详情页布局
@app.route('/')def base(): context = { 'questions': Question.query.all() } return render_template("base.html",**context) {% for foo in questions %}
- { {foo.author.username}} { { foo.title }} { { foo.creat_time }}
{
{ foo.detail}} { % endfor %}
十一、从首页问答标题到问答详情
@app.route('/detail/')def detail(question_id): quest= Question.query.filter(Question.id == question_id).first() return render_template('detail.html',ques=quest) { {foo.author.username}} { { foo.title }} { { foo.creat_time }} {
{ foo.detail}} 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')) create_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')){ % extends'fumoban.html' %}{ % block title %}评论详情{% endblock %} { % block head %} { % endblock %}{ % block main %}{ % endblock %}{ { ques.author.username }} { { ques.title }} { { ques.create_time }}
{
{ ques.detail }}
十二、完成评论功能
@app.route('/detail/')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_detail = 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(detail=comment, author_id=auth_id, question_id=ques_id, ) db.session.add(comment) db.session.commit() return redirect(url_for('detail', question_id=ques_id))def loginFrist(func): @wraps(func) def wrapper(*args, **kwargs): if session.get('user'): return func(*args, **kwargs) else: return redirect(url_for('login')) return wrapper复制代码复制代码 { { ques.title }}{ { ques.author.username }}{ { ques.create_time }}
{
{ ques.detail }}
评论.({ { ques.comments|length }})
{ % endblock %}
十三、评论列表及排序,个人中心显示
@app.route('/userCenter/', methods=['GET'])def userCenter(user_id): user = User.query.filter(User.id == user_id).first() context = { 'username': user.username, 'questions': user.question, 'comments': user.comment } return render_template('userCenter.html', **context){ % extends 'fumoban.html' %}{ % block title %}个人中心 {% endblock%}{ % block main%} { {username}} detail comment
{ % for foo in user.question %}
- { {foo.author.username }} { {foo.creat_time}}
{ % endfor %}{
{foo.detail}}{ {user}} personal information
{ % for foo in user.comments %}
- { {foo.author.username }} { {foo.creat_time}}
{ % endfor %}{
{foo.detail}}{ % endblock %}{ {user}} personal information
- user:{ {username}}
- time:
- nick:
- context:
十四、个人中心标签页导航
{% extends "个人中心.html" %}{ % block usercenterbody %}{ { username }}问答
{ % for foo in comments %}
- { { foo.author.username }} { { foo.creat_time }}
{ % endfor %}{
{ foo.detail }}{ { user }}评论
{ % for foo in comments %}
- { { foo.author.username }} { { foo.creat_time }}
{ % endfor %}{
{ foo.detail }}{ % endblock %}{ { user }}个人资料
- user:{ { username }}
- time:
- nick:
十五、完成个人中心——导航标签
@app.route('/usercenter// ')@logdef usercenter(user_id,tag): user=User.query.filter(User.id==user_id).first() context={ 'username':user.username, 'questions':user.questions, 'comments':user.comments } if tag == '1': return render_template('usercenter1.html', **context) elif tag == '2': return render_template('usercenter2.html', **context) else: return render_template('usercenter3.html', **context)
十六、实现搜索功能
@app.route('/search/')def search(): qu=request.args.get('q') ques=Question.query.filter( or_( Question.title.contains(qu), Question.detail.contains(qu), ) ).order_by('-creat_time') return render_template('base.html',questions=ques)
十七、密码保护
from werkzeug.security import generate_password_hash,check_password_hash app=Flask(__name__)app.config.from_object(config)db=SQLAlchemy(app) # 创建用户模型class User(db.Model): __tablename__ = '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)#内部使用 nickname=db.Column(db.String(50))@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): result = check_password_hash(self._password,row_password) return result@app.route('/login/',methods={ 'GET','POST'})def login(): if request.method=='GET': return render_template('login.html') else: username = request.form.get('username') # 获取form中的数据 password = request.form.get('password') nickname = request.form.get('nickname') user = User.query.filter(User.username == username).first() # id = User.query.filter(User.id == id).first() if user: if user.check_password(password): session['user'] = username # session['id'] = id session.permanent=True return redirect(url_for('index')) # 返回到首页 else: return u'password error' else: return u'error username or password' #输出错误