一、个人学期总结
Python,是一种面向对象的解释计算机程序设计语言,由荷兰人Guido van Rossum于1989年发明,第一个公开发行版发行于1991年。
             Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议[2]  。Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进。
Python具有丰富和强大的库。它常被昵称为胶水语言,能够把用其他语言制作的各种模块,很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中[3]  有特别要求的部分,用更合适的语言改写,比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C/C++重写,而后封装为Python可以调用的扩展类库。需要注意的是在您使用扩展类库时可能需要考虑平台问题,某些可能不提供跨平台的实现。
在这个学期,我们学习了这门语言,感觉比java之类的编程语言要简单的多,学习到了turtle库的基础练习、字符串的基本操作、凯撒密码 、GDP格式化输出、99乘法表、中英文词频统计、和datetime处理日期和时间等等,我知道,这只是个开始,我们学的还很浅薄,而我还没有完全消化点这点浅薄的知识,为此我还要继续努力,为未来的深入了解打好基础。
二、Python+Flask+MysqL的web建设技术过程总结
1.web建设使用工具
pycharm+mysql+Python+Navicat for mysql

2.基础界面展示
首页

注册页

登录页

发布页面

个人中心

评论页面

3.部分代码
第三方库导入与数据库的连接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 from sqlalchemy import or_, and_ from werkzeug.security import generate_password_hash,check_password_hash
app = Flask(__name__) # 创建Flask对象 app.config.from_object(config) # 关联config.py文件进来 db = SQLAlchemy(app) # 建立和数据库的关系映射
数据库表的建立与增删查改功能
登录与注册功能的实现
py
@app.route('/') # 跳转首页。 def daohang(): context = { 'fabus': Fabu.query.order_by('-creat_time').all() # order_by('-creat_time')按时间降序排列,Fabu.query.all()查出了Fabu类的所有元组 } return render_template('daohang.html', **context) @app.route('/lin/') # 跳转测试。 def lin(): return 'lin' # 跳转登陆。 @app.route('/denglu/', methods=['GET', 'POST']) # methods定义它有两种请求方式 def denglu(): if request.method == 'GET': return render_template('denglu.html') else: username = request.form.get('user') # post请求模式,安排对象接收数据 password = request.form.get('pass') user = User.query.filter(User.username == username).first() # 作查询,并判断 if user: # 判断用户名 if user.check_password(password): # 判断密码 session['user'] = username # 利用session添加传回来的值username session.permanent = True # 设置session过期的时间 return redirect(url_for('daohang')) else: return u'用户密码错误' else: return u'用户不存在,请先注册'
页面获取信息
登录
<form action="{{ url_for('denglu') }}" method="post"> 登陆名:<input type="text" name="user" id="user" placeholder="请输入用户名"> <br> 密码:<input type="password" name="pass" id="pass" placeholder="请输入密码"> <br> <input type="radio" name="r1" id="r1" value="stu">学生 <input type="radio" name="r2" id="r2" value="tea">教授 <br> <input type="checkbox" name="c1" id="c1" value="">记住密码 <br> <div id="error_box"><br></div> <input type="submit" value="登录" onclick="return fnLogin()">&jk <button><a href="http://127.0.0.1:5000/zhuce/">注册</a> </button>
#注册
<div id="header"><h2 align="center">注册</h2></div> <div id="content"> <form action="{{ url_for('zhuce') }}" method="post"> <p align="center">登陆名:</p> <p align="center"> <input type="text" name="user" id="user" placeholder="请输入用户名"> </p> <p align="center">密码:</p> <p align="center"> <input type="password" name="pass" id="pass" placeholder="请输入密码"> </p> <p align="center">确认密码:</p> <p align="center"> <input type="password" name="again" id="again" placeholder="再次输入密码"> </p>
发布详情页与发布评论
# 跳转发布。 @app.route('/fabu/', methods=['GET', 'POST']) @loginFirst # 将decorator定义的增强函数放在待增强函数定义的上面 def fabu(): if request.method == 'GET': return render_template('fabu.html') else: title = request.form.get('title') # post请求模式,安排对象接收数据 detail = request.form.get('detail') author_id = User.query.filter( User.username == session.get('user')).first().id fabu = Fabu(title=title, detail=detail, author_id=author_id) # 将对象接收的数据赋到Fabu类中,即存到数据库 db.session.add(fabu) db.session.commit() return redirect(url_for('daohang')) # redirect重定向 # 跳转发布详情 @app.route('/fabuview/<fabu_id>') def fabuview(fabu_id): fa = Fabu.query.filter(Fabu.id == fabu_id).first() comments = Comment.query.filter(Comment.fabu_id == fabu_id).all() return render_template('fabuview.html', fa=fa, comments=comments) # 跳转评论 @app.route('/comment/', methods=['POST']) @loginFirst # 装饰器,跳转某页面之前先进行登录 def comment(): detail = request.form.get('pinglun') # post请求模式,安排对象接收数据 author_id = User.query.filter(User.username == session.get('user')).first().id fabu_id = request.form.get('fa_id') comment = Comment(detail=detail, author_id=author_id, fabu_id=fabu_id) db.session.add(comment) # 执行操作 db.session.commit() # 提交到数据库 return redirect(url_for('fabuview', fabu_id=fabu_id)) # 重定向到fabuview请求时要带fabu_id
个人中心
py
app.route('/yonghu/<username_id>/<tag>')
def yonghu(username_id, tag):
    user = User.query.filter(User.id == username_id).first()
    context = {
        'userid': user.id,
        'username': user.username,
        'fabus': user.fabu,
        'comments': user.comments
    }  # 根据tag的不同去到不同页面,一个请求跳转3个不同页面
    if tag == '1':
        return render_template('yonghu1.html', **context)
    elif tag == '2':
        return render_template('yonghu2.html', **context)
    else:
        return render_template('yonghu3.html', **context)
搜索功能
py
@app.route('/search/')
def search():
    sousuo = request.args.get('sousuo')  # args获取关键字,区别form
    fabus = Fabu.query.filter(
        or_(                               # 两种查询条件
            Fabu.title.contains(sousuo),   # contains模糊查
            Fabu.detail.contains(sousuo)
        )
    ).order_by('-creat_time')
    return render_template('daohang.html', fabus=fabus)   # fabus要和原首页数据模型一样
密码保护
@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
 
                    
                     
                    
                 
                    
                 
 
        
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号