新手在学flask开发,看到《python web开发》第十章的用户资料,
在搞 管理员级别的资料编辑器 那里出问题了,
视图函数抛出异常,不能返回响应
百度,谷歌,sof无果。找好久了没解决,心塞
所以来寻求帮助,谢谢能提供帮助的人
这是整个文件夹
http://pan.baidu.com/s/1kV4sXcr
@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 = EditProfileAdminForm(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)
flash('The profile has been updated.')
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)
class EditProfileAdminForm(FlaskForm):
email = StringField('Email', validators=[Required(), Length(1, 64),
Email()])
username = StringField('Username', validators=[
Required(), Length(1, 64), Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
'Username must have only letters,'
'number, dots or underscores')])
confirmed = BooleanField('Confirmed')
role = SelectField('Role', coerce=int)
name = StringField('Real name', validators=[Length(0, 64)])
location = StringField('Location', validators=[Length(0, 64)])
about_me = TextAreaField('About me')
submit = SubmitField('Submit')
def __init__(self, user, *args, **kwargs):
super(EditProfileAdminForm, 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 validate_email(self, field):
if field.data != self.user.email and \
User.query.filter_by(email=field.date).first():
raise ValidationError('Emai already registered.')
def validate_username(self, field):
if field.data !=self.user.username and \
User.query.filter_by(username=field.data).first():
raise ValidationError('Username already in use.')
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Flasky - Edit Profile{% endblock %}
{% block page_content %}
<p class="page-header">
<h1>Edit Your Profile</h1>
</p>
<p class="col-md-4">
{{ wtf.quick_form(form) }}
</p>
{% endblock %}
怪我咯2017-04-18 10:25:02
Netizens helped find the problem. There is a problem with the custom decorator that checks user permissions decorators.py:
def permission_required(permission):
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not current_user.can(permission):
abort(403)
return f(*args, **kwargs) # 此行缩进错误,应在if语句外
return decorated_function
return decorator
Just modify it.
T.T, I am really careless.
Python code indentation problem, I checked it specifically. Netizens suggested using 4 spaces for indentation. It is too risky to use 1 tab key instead, because each editor has different definitions of the tab key, and it is easy to cause problems. . Although my problem is not with the tab key, I will still pay attention to this problem in the future.
But I still have a question, why does the error page throw a problem with the view function,
but not an error on the decorator side, for example (IndentationError):
@admin_required → def permission_required(permission)
Thank you