您现在的位置是: 网站首页 >Flask >Flask搭建微电影视频网站 Flask

【Flask微电影】25.会员中心登录要求,会员资料和密码修改

admin2018年11月12日 21:53 Flask | Html | Python 418人已围观

Flask搭建微电影视频网站简介 利用Flask搭建微电影视频网站 Github地址:https://github.com/xyliurui/FlaskMovie

### 会员登录装饰器 #### 在home.views.py中创建登录装饰器 ```python from functools import wraps # 要求登录才能访问 def user_login_require(func): @wraps(func) def decorated_function(*args, **kwargs): if session.get('login_user', None) is None: # 如果session中未找到该键,则用户需要登录 return redirect(url_for('home.login', next=request.url)) return func(*args, **kwargs) return decorated_function ``` #### 增加登录装饰器 ```python @home.route('/user/') @user_login_require def user(): return render_template('home/user.html') ``` 也需要将以下功能添加要求登录。 ![BLOG_20181112_215345_11](/media/blog/images/2018/11/BLOG_20181112_215345_11.png "博客图集BLOG_20181112_215345_11.png") ### 会员资料修改 #### 创建会员资料表单 ```python class UserDetailForm(FlaskForm): name = StringField( label='昵称', validators=[ DataRequired('请输入昵称!') ], description='昵称', render_kw={ 'class': "form-control", 'placeholder': "请输入昵称", 'required': "required", 'autofocus': "autofocus" } ) email = StringField( label='邮箱', validators=[ DataRequired('请输入邮箱!'), Email('邮箱格式不正确') ], description='邮箱', render_kw={ 'class': "form-control", 'placeholder': "请输入邮箱", 'required': "required", 'autofocus': "autofocus" } ) phone = StringField( label='手机', validators=[ DataRequired('请输入手机!'), Regexp('^1[3|4|5|6|7|8][0-9]\d{4,8}$', message='手机格式不正确') ], description='手机', render_kw={ 'class': "form-control", 'placeholder': "请输入手机", 'required': "required", 'autofocus': "autofocus" } ) face = FileField( label='头像', validators=[ DataRequired('请上传头像') ], description='头像', ) info = TextAreaField( label='简介', validators=[ DataRequired('请输入简介!') ], description='简介', render_kw={ 'class': "form-control", 'rows': "10", } ) submit = SubmitField( label='保存', render_kw={ 'class': "btn btn-success" } ) ``` #### 修改user会员资料视图 ```python from werkzeug.utils import secure_filename import os from app import db, app @home.route('/user/', methods=['GET', 'POST']) @user_login_require def user(): login_user = User.query.get_or_404(int(session['login_user_id'])) form = UserDetailForm( name=login_user.name, email=login_user.email, phone=login_user.phone, info=login_user.info ) form.face.validators = [] form.face.render_kw = {'required': False} if form.validate_on_submit(): data = form.data face_save_path = app.config['USER_IMAGE'] if not os.path.exists(face_save_path): os.makedirs(face_save_path) # 如果文件保存路径不存在,则创建一个多级目录 import stat os.chmod(face_save_path, stat.S_IRWXU) # 授予可读写权限 if form.face.data: # 上传文件不为空保存 if login_user.face and os.path.exists(os.path.join(face_save_path, login_user.face)): os.remove(os.path.join(face_save_path, login_user.face)) # 获取上传文件名称 file_face = secure_filename(form.face.data.filename) # !!!AttributeError: 'str' object has no attribute 'filename',前端需要加上enctype="multipart/form-data" from app.admin.views import change_filename login_user.face = change_filename(file_face) form.face.data.save(face_save_path + login_user.face) if login_user.name != data['name'] and User.query.filter_by(name=data['name']).count() == 1: flash('昵称已经存在', 'err') return redirect(url_for('home.user')) login_user.name = data['name'] if login_user.email != data['email'] and User.query.filter_by(email=data['email']).count() == 1: flash('邮箱已经存在', 'err') return redirect(url_for('home.user')) login_user.email = data['email'] if login_user.phone != data['phone'] and User.query.filter_by(phone=data['phone']).count() == 1: flash('手机号已经存在', 'err') return redirect(url_for('home.user')) login_user.phone = data['phone'] login_user.info = data['info'] db.session.commit() flash('修改资料成功', 'ok') return redirect(url_for('home.user')) return render_template('home/user.html', form=form, login_user=login_user) ``` #### 修改user.html会员资料模板 ```html <div class="panel-body"> {% with msgs = get_flashed_messages(category_filter=['ok']) %} {% if msgs %} {% for msg in msgs %} <p>{{ msg }}</p> {% endfor %} {% endif %} {% endwith %} {% with msgs = get_flashed_messages(category_filter=['err']) %} {% if msgs %} {% for msg in msgs %} <p>{{ msg }}</p> {% endfor %} {% endif %} {% endwith %} <form role="form" method="post" enctype="multipart/form-data"> <fieldset> <div class="form-group"> <label for="input_name"><span class="glyphicon glyphicon-user"></span>&nbsp;{{ form.name.label }}</label> {{ form.name }} </div> {% for err in form.name.errors %} <div class="col-md-12" style="color: red">{{ err }}</div> {% endfor %} <div class="form-group"> <label for="input_email"><span class="glyphicon glyphicon-envelope"></span>&nbsp;{{ form.email.label }}</label> {{ form.email }} </div> {% for err in form.email.errors %} <div class="col-md-12" style="color: red">{{ err }}</div> {% endfor %} <div class="form-group"> <label for="input_phone"><span class="glyphicon glyphicon-phone"></span>&nbsp;{{ form.phone.label }}</label> {{ form.phone }} </div> {% for err in form.phone.errors %} <div class="col-md-12" style="color: red">{{ err }}</div> {% endfor %} <div class="form-group"> <label for="input_face"><span class="glyphicon glyphicon-picture"></span>&nbsp;{{ form.face.label }}</label> {% if login_user.face %} <img src="{{ url_for('static', filename='user/'+login_user.face) }}" class="img-responsive img-rounded" style="width: 100px"> {% else %} <img data-src="holder.js/100x100" class="img-responsive img-rounded"> {% endif %} {{ form.face }} </div> {% for err in form.face.errors %} <div class="col-md-12" style="color: red">{{ err }}</div> {% endfor %} <div class="form-group"> <label for="input_info"><span class="glyphicon glyphicon-edit"></span>&nbsp;{{ form.info.label }}</label> {{ form.info }} </div> {% for err in form.info.errors %} <div class="col-md-12" style="color: red">{{ err }}</div> {% endfor %} {{ form.csrf_token }} {{ form.submit }} </fieldset> </form> </div> ``` ![BLOG_20181112_215403_90](/media/blog/images/2018/11/BLOG_20181112_215403_90.png "博客图集BLOG_20181112_215403_90.png") ### 会员密码修改 #### 创建会员密码修改表单 ```python class PwdForm(FlaskForm): oldpwd = PasswordField( label='旧密码', validators=[ DataRequired('请输入旧密码!') ], description='旧密码', render_kw={ 'class': "form-control", 'placeholder': "请输入旧密码", 'required': "required", 'autofocus': 'autofocus' } ) newpwd = PasswordField( label='新密码', validators=[ DataRequired('请输入新密码!') ], description='新密码', render_kw={ 'class': "form-control", 'placeholder': "请输入新密码", 'required': "required", 'autofocus': 'autofocus' } ) repwd = PasswordField( label='重复密码', validators=[ DataRequired('请输入重复密码!'), EqualTo('newpwd', message='两次密码不一致') ], description='重复密码', render_kw={ 'class': "form-control", 'placeholder': "请输入重复密码", 'required': "required" } ) submit = SubmitField( label='修改密码', render_kw={ 'class': "btn btn-success" } ) ``` #### 修改pwd修改密码视图 ```python @home.route('/pwd/', methods=['GET', 'POST']) @user_login_require def pwd(): login_user = User.query.get_or_404(int(session['login_user_id'])) form = PwdForm() if form.validate_on_submit(): data = form.data if login_user.check_pwd(data['oldpwd']): login_user.pwd = generate_password_hash(data['newpwd']) db.session.commit() flash('密码修改成功,请重新登录', category='ok') return redirect(url_for('home.login')) else: flash('旧密码不正确', category='err') return redirect(url_for('home.pwd')) return render_template('home/pwd.html', form=form) ``` #### 修改pwd.html修改密码模板 ```html <div class="panel-body"> {% with msgs = get_flashed_messages(category_filter=['ok']) %} {% if msgs %} {% for msg in msgs %} <p>{{ msg }}</p> {% endfor %} {% endif %} {% endwith %} {% with msgs = get_flashed_messages(category_filter=['err']) %} {% if msgs %} {% for msg in msgs %} <p>{{ msg }}</p> {% endfor %} {% endif %} {% endwith %} <form role="form" method="post"> <fieldset> <div class="form-group"> <label for="input_oldpwd"><span class="glyphicon glyphicon-lock"></span>&nbsp;{{ form.oldpwd.label }}</label> {{ form.oldpwd }} </div> {% for err in form.oldpwd.errors %} <div class="col-md-12" style="color: red">{{ err }}</div> {% endfor %} <div class="form-group"> <label for="input_newpwd"><span class="glyphicon glyphicon-lock"></span>&nbsp;{{ form.newpwd.label }}</label> {{ form.newpwd }} </div> {% for err in form.newpwd.errors %} <div class="col-md-12" style="color: red">{{ err }}</div> {% endfor %} <div class="form-group"> <label for="input_newpwd"><span class="glyphicon glyphicon-lock"></span>&nbsp;{{ form.repwd.label }}</label> {{ form.repwd }} </div> {% for err in form.repwd.errors %} <div class="col-md-12" style="color: red">{{ err }}</div> {% endfor %} {{ form.csrf_token }} {{ form.submit }} </fieldset> </form> </div> ``` ![BLOG_20181112_215427_86](/media/blog/images/2018/11/BLOG_20181112_215427_86.png "博客图集BLOG_20181112_215427_86.png")

很赞哦! (0)

文章交流

  • emoji
0人参与,0条评论

当前用户

未登录,点击   登录

站点信息

  • 建站时间:网站已运行590天
  • 系统信息:Linux
  • 后台程序:Python: 3.6.6
  • 网站框架:Django: 2.2.9
  • 文章统计:222 篇
  • 文章评论:40 条
  • 腾讯分析网站概况-腾讯分析
  • 百度统计网站概况-百度统计
  • 微信公众号:扫描二维码,关注我们
返回
顶部
标题 换行 登录
网站