flask-用户资料
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask-用户资料相关的知识,希望对你有一定的参考价值。
首先创建User模型
class User(UserMixin,db.Model): __tablename__ = ‘users‘
#..
name = db.Column(db.String(64)) location = db.Column(db.String(64)) about_me = db.Column(db.Text()) member_since = db.Column(db.DateTime(),default=datetime.utcnow) last_seen = db.Column(db.DateTime(),default=datetime.utcnow)
刷新用户访问时间
def ping(self): self.last_seen = datetime.utcnow() db.session.add(self)
app/auth/views.py :更新已登录用户的访问时间
@auth.before_app_request # def before_request(): # if current_user.is_authenticated: # current_user.ping() # if not current_user.confirmed # and request.endpoint[:5] != ‘auth.‘ : # return redirect(url_for(‘auth.unconfirmed‘))
用户资料页面
app/main/views.py
@main.route(‘/user/<username>‘) def user(username): user = User.query.filter_by(username=username).first() if user is None: abort(404) return render_template(‘user.html‘, user=user)
用户资料页面的模板
{% block page_content %} <div class="page-header"> <h1>{{ user.username }}</h1> </div> {% if user.name or user.location %} <p> {% if user.name %}{{ user.name }}{% endif %} {% if user.about_me %}<p>{{ user.about_me }}</p>{% endif %} {% if user.location %} From<a href="http://maps.google.com/?q={{ user.location }}">{{ user.location }}</a> {% endif %} </p> {% endif %} {% if current_user.is_administrator() %} <p><a href="mailto:{{ user.email }}" >{{ user.email }}</a></p> <a class="btn btn-danger" href="{{ url_for(‘.edit_profile_admin‘,id=user.id) }}">Edit</a> {% endif %} <p> Member since {{ moment(user.member_since).format(‘L‘) }}. Last seen {{ moment(user.last_seen).fromNow() }} </p> {% endblock %}
创建链接
{% if current_user.is_authenticated %} <li><a href="{{ url_for(‘main.user‘,username=current_user.username) }}">个人资料</a></li> {% endif %}
用户级别的资料编辑器
class EditProfieForm(FlaskForm): name = StringField(u‘昵称‘,validators=[Length(0,64)]) location = StringField(u‘地址‘,validators=[Length(0,64)]) about_me = TextAreaField(u‘个性签名‘) submit = SubmitField(u‘提交‘)
资料编辑页面
app/main/views.py
@main.route(‘/edit-profile‘,methods=[‘GET‘,‘POST‘])
@login_required
def edit_profile():
form = EditProfieForm()
if form.validate_on_submit():
current_user.name = form.name.data
current_user.location = form.location.data
current_user.about_me = form.about_me.data
db.session.add(current_user)
db.session.commit()
flash(u‘更新成功‘)
return redirect(url_for(‘.user‘,username=current_user.username))
form.name.data = current_user.name
form.location.data = current_user.location
form.about_me.data = current_user.about_me
return render_template(‘edit_profile.html‘, form=form)
资料编辑的链接
{% if user == current_user %} <a class="btn btn-danger" href="{{ url_for(‘.edit_profile_admin‘,id=user.id) }}">Edit</a> {% endif %}
管理员级别的资料编辑表单
app/main/forms.py
class EditProfieAdminForm(FlaskForm): email = StringField(u‘邮箱‘,validators=[Required(),Length(1,64),Email()]) username = StringField(u‘用户名‘,validators=[Required(),Length(1,64),Regexp(‘^[A-Za-z][A-Za-z0-9_.]*$‘,0,u‘用户名必须只有字母,数字,.,和下划线‘)]) confirmed = BooleanField(‘Confirmed‘) role = SelectField(‘Role‘,coerce=int) name = StringField(u‘昵称‘,validators=[Length(0,64)]) location = StringField(u‘地址‘,validators=[Length(0,64)]) about_me = TextAreaField(u‘个性签名‘) submit = SubmitField(u‘提交‘) def __init__(self,user,*args,**kwargs): super(EditProfieAdminForm,self).__init__(*args,**kwargs) self.role.choices = [(role.id,role.name) for role in Role.query.order_by(Role.name).all()] self.user = user def valideate_email(self,field): if field.data != self.user.username and User.query.filter_by(username=field.data).first(): raise ValueError(‘用户名已在使用‘)
管理员级别的资料编辑路由
@main.route(‘/edit-profile/<int:id>‘,methods=[‘GET‘,‘POST‘]) @login_required @admin_required def edit_profile_admin(id): user = User.query.get_or_404(id) form = EditProfieAdminForm(user=user) if form.validate_on_submit(): user.email = form.email.data user.username = form.username.data user.confirmed = form.confirmed.data user.role = Role.query.get(form.role.data) user.name = form.name.data user.location = form.location.data user.about_me = form.about_me.data db.session.add(user) db.session.commit() flash(u‘更新成功‘) return redirect(url_for(‘.user‘,username=user.username)) form.email.data = user.email form.username.data = user.username form.confirmed.data = user.confirmed form.role.data = user.role_id form.name.data = user.name form.location.data = user.location form.about_me.data = user.about_me return render_template(‘edit_profile.html‘,form=form,user=user)
管理员使用的资料编辑链接
{% if current_user.is_administrator() %} {% if user == current_user %} <a class="btn btn-danger" href="{{ url_for(‘.edit_profile_admin‘,id=user.id) }}">Edit</a> {% endif %}
以上是关于flask-用户资料的主要内容,如果未能解决你的问题,请参考以下文章