新手在学 flask 开发 T.T ,看到《 python web 开发》第十章的用户资料, 在搞 管理员级别的资料编辑器 那里出问题了, 视图函数抛出异常,不能返回响应 。百度,谷歌, sof 无果。找好久了没解决,心塞 所以来寻求帮助,谢谢能提供帮助的人。 这是整个文件夹 http://pan.baidu.com/s/1kV4sXcr
ValueError
ValueError: View function did not return a response
Traceback (most recent call last)
File "D:\web_develop\venv\lib\site-packages\flask-0.12-py2.7.egg\flask\app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "D:\web_develop\venv\lib\site-packages\flask-0.12-py2.7.egg\flask\app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "D:\web_develop\venv\lib\site-packages\flask-0.12-py2.7.egg\flask\app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "D:\web_develop\venv\lib\site-packages\flask-0.12-py2.7.egg\flask\app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "D:\web_develop\venv\lib\site-packages\flask-0.12-py2.7.egg\flask\app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "D:\web_develop\venv\lib\site-packages\flask-0.12-py2.7.egg\flask\app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "D:\web_develop\venv\lib\site-packages\flask-0.12-py2.7.egg\flask\app.py", line 1725, in make_response
raise ValueError('View function did not return a response')
ValueError: View function did not return a response
##路由视图 views.py :
@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)
##表单 forms.py:
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.')
##模板 edit_profile.html :
{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% block title %}Flasky - Edit Profile{% endblock %}
{% block page_content %}
<div class="page-header">
<h1>Edit Your Profile</h1>
</div>
<div class="col-md-4">
{{ wtf.quick_form(form) }}
</div>
{% endblock %}
1
fy 2017-03-02 00:13:15 +08:00
没渲染到模板?楼主试试 ret = render_template('edit_profile.html', form=form, user=user)
然后 print 看看 ret 里头是啥再 return 可能是路径配错了。 另外貌似楼主所有的缩进都是 TAB ,而不是空格。 |
2
flaneurse 2017-03-02 02:47:34 +08:00 via Android
为啥不去作者 github 问
|
3
BMYY OP @fy 谢谢你的回答
试了你的方法抛出错误一样,没 print 出啥东西。 其他网友找到问题了,检查用户权限的自定义装饰器出问题了 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 ``` 修改过来就可以了。 T.T ,真是自己粗心大意。 python 代码缩进问题,我特意去查了,网友都建议缩进用 4 个空格,用 1 个 tab 代替风险太大,因为各家的编辑器对 tab 键定义存在差异,很容易出问题。虽然说我这问题不是出在 tab 上,之后我还是会注意这个问题的。感谢。 不过我还有一问题,为什么错误页面抛出的是 视图函数 View function 的问题, 而不会抛出装饰器那边的错误,例如( IndentationError ): @admin_required → def permission_required(permission) 谢谢 |
4
BMYY OP |