AttributeError: 'tuple' object has no attribute 'strip' : Flask Markdown?
AttributeError: 'tuple' object has no attribute 'strip' : Flask Markdown?
我一直在关注 Miguel Grinberg 的 Flask Web 开发。在构建博客时,我无法理解(我收到的)错误是什么,以及它是由于我的代码还是由于我正在使用的扩展引起的。
File "/home/abhinav/projects/sample/app/blog/views.py", line 51, in edit
blog.body=form.body.data,
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 224, in __set__
instance_dict(instance), value, None)
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 696, in set
value, old, initiator)
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 705, in fire_replace_event
self._init_append_or_replace_token())
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/sqlalchemy/orm/events.py", line 1534, in wrap
fn(target, value, *arg)
File "/home/abhinav/projects/sample/app/models.py", line 368, in on_changed_body
markdown(value, output_format='html'),
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/markdown/__init__.py", line 494, in markdown
return md.convert(text)
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/markdown/__init__.py", line 355, in convert
if not source.strip():
AttributeError: 'tuple' object has no attribute 'strip'
为了插入图片和其他内容以作为 HTML 发布,我在我的 models.py 文件中使用了以下代码片段:
我从 here.
中获得了部分片段
class Blog(db.Model):
__tablename__ = 'blogs'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(64), unique=True, index=True)
body = db.Column(db.Text)
summary = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
body_html = db.Column(db.Text)
summary_html = db.Column(db.Text)
@staticmethod
def on_changed_body(target, value, oldvalue, initiator):
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code',
'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul',
'h2', 'h3', 'p', 'img', 'video', 'div', 'iframe', 'p', 'br', 'span', 'hr', 'src', 'class']
allowed_attrs = {'*': ['class'],
'a': ['href', 'rel'],
'img': ['src', 'alt']}
target.body_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=False, attributes=allowed_attrs))
@staticmethod
def on_changed_summary(target, value, oldvalue, initiator):
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i',
'strong']
target.summary_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=True))
db.event.listen(Blog.body, 'set', Blog.on_changed_body)
db.event.listen(Blog.summary, 'set', Blog.on_changed_summary)
我还在自己的字段中看到了一些不需要的字符。这些字符可以在标题字段以及博客条目内容部分中看到。这也被注意到here.
编辑
我正在添加我的功能
def edit(id):
blog = Blog.query.get_or_404(id)
if current_user != blog.author and \
not current_user.can(Permission.WRITE_BLOG_ARTICLES):
abort(403)
form = BlogForm()
if form.validate_on_submit():
blog.title=form.title.data,
blog.body=form.body.data,
blog.summary=form.summary.data
db.session.add(blog)
flash('The blog has been updated.')
return redirect(url_for('.entry', id=blog.id))
form.title.data = blog.title,
form.body.data = blog.body,
form.summary.data = blog.summary
return render_template('blog/edit_blog_post.html', form=form)
你在flask中使用interactive debugger吗?它允许您检查回溯的每个级别并查看变量是什么。
如果您使用调试器触发错误,请展开回溯的这一行:
File "/home/abhinav/projects/sample/app/models.py", line 368, in on_changed_body
markdown(value, output_format='html'),
并检查 value
的内容,我猜它是一个元组而不是字符串。
您也可以在此行之前使用 print
语句来检查内容:
...original code...
print value
target.summary_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=True))
...
(但我真的推荐调试器超过 print
调试!它更好)
如果 value
确实是一个字符串,那么您可以在回溯的周围级别重复相同的过程。这将告诉您它是否在 markdown 包中被转换为元组,这可能是库本身的错误,或者您的代码中是否存在其他问题。这种调试方法可以让你从零定位问题所在。
至于你关于 "undesired characters" 的说明,你是这个意思吗?
(u'Block quote should finally work.. Well',)
如果您在最终输出中看到这一点,则意味着您以某种方式在某处传递元组而不是字符串。
>>> title = (u'Block quote should finally work.. Well',)
>>> type(title)
<type 'tuple'>
>>> title.strip()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'strip'
#vs..
>>> title = u'Block quote should finally work.. Well'
>>> type(title)
<type 'unicode'>
>>> title.strip()
u'Block quote should finally work.. Well'
响应您发布的新代码,请查看这些行:
form.title.data = blog.title,
form.body.data = blog.body,
结尾的逗号创建一个元素的元组:
>>> 'a',
('a',)
>>> x = 'a',
>>> type(x)
<type 'tuple'>
删除多余的逗号,这应该至少可以解决您的一个问题。
我一直在关注 Miguel Grinberg 的 Flask Web 开发。在构建博客时,我无法理解(我收到的)错误是什么,以及它是由于我的代码还是由于我正在使用的扩展引起的。
File "/home/abhinav/projects/sample/app/blog/views.py", line 51, in edit
blog.body=form.body.data,
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 224, in __set__
instance_dict(instance), value, None)
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 696, in set
value, old, initiator)
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 705, in fire_replace_event
self._init_append_or_replace_token())
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/sqlalchemy/orm/events.py", line 1534, in wrap
fn(target, value, *arg)
File "/home/abhinav/projects/sample/app/models.py", line 368, in on_changed_body
markdown(value, output_format='html'),
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/markdown/__init__.py", line 494, in markdown
return md.convert(text)
File "/home/abhinav/projects/avenues-flask/venv/lib/python2.7/site-packages/markdown/__init__.py", line 355, in convert
if not source.strip():
AttributeError: 'tuple' object has no attribute 'strip'
为了插入图片和其他内容以作为 HTML 发布,我在我的 models.py 文件中使用了以下代码片段: 我从 here.
中获得了部分片段class Blog(db.Model):
__tablename__ = 'blogs'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(64), unique=True, index=True)
body = db.Column(db.Text)
summary = db.Column(db.Text)
timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
body_html = db.Column(db.Text)
summary_html = db.Column(db.Text)
@staticmethod
def on_changed_body(target, value, oldvalue, initiator):
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code',
'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul',
'h2', 'h3', 'p', 'img', 'video', 'div', 'iframe', 'p', 'br', 'span', 'hr', 'src', 'class']
allowed_attrs = {'*': ['class'],
'a': ['href', 'rel'],
'img': ['src', 'alt']}
target.body_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=False, attributes=allowed_attrs))
@staticmethod
def on_changed_summary(target, value, oldvalue, initiator):
allowed_tags = ['a', 'abbr', 'acronym', 'b', 'code', 'em', 'i',
'strong']
target.summary_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=True))
db.event.listen(Blog.body, 'set', Blog.on_changed_body)
db.event.listen(Blog.summary, 'set', Blog.on_changed_summary)
我还在自己的字段中看到了一些不需要的字符。这些字符可以在标题字段以及博客条目内容部分中看到。这也被注意到here.
编辑
我正在添加我的功能
def edit(id):
blog = Blog.query.get_or_404(id)
if current_user != blog.author and \
not current_user.can(Permission.WRITE_BLOG_ARTICLES):
abort(403)
form = BlogForm()
if form.validate_on_submit():
blog.title=form.title.data,
blog.body=form.body.data,
blog.summary=form.summary.data
db.session.add(blog)
flash('The blog has been updated.')
return redirect(url_for('.entry', id=blog.id))
form.title.data = blog.title,
form.body.data = blog.body,
form.summary.data = blog.summary
return render_template('blog/edit_blog_post.html', form=form)
你在flask中使用interactive debugger吗?它允许您检查回溯的每个级别并查看变量是什么。
如果您使用调试器触发错误,请展开回溯的这一行:
File "/home/abhinav/projects/sample/app/models.py", line 368, in on_changed_body
markdown(value, output_format='html'),
并检查 value
的内容,我猜它是一个元组而不是字符串。
您也可以在此行之前使用 print
语句来检查内容:
...original code...
print value
target.summary_html = bleach.linkify(bleach.clean(
markdown(value, output_format='html'),
tags=allowed_tags, strip=True))
...
(但我真的推荐调试器超过 print
调试!它更好)
如果 value
确实是一个字符串,那么您可以在回溯的周围级别重复相同的过程。这将告诉您它是否在 markdown 包中被转换为元组,这可能是库本身的错误,或者您的代码中是否存在其他问题。这种调试方法可以让你从零定位问题所在。
至于你关于 "undesired characters" 的说明,你是这个意思吗?
(u'Block quote should finally work.. Well',)
如果您在最终输出中看到这一点,则意味着您以某种方式在某处传递元组而不是字符串。
>>> title = (u'Block quote should finally work.. Well',)
>>> type(title)
<type 'tuple'>
>>> title.strip()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'strip'
#vs..
>>> title = u'Block quote should finally work.. Well'
>>> type(title)
<type 'unicode'>
>>> title.strip()
u'Block quote should finally work.. Well'
响应您发布的新代码,请查看这些行:
form.title.data = blog.title,
form.body.data = blog.body,
结尾的逗号创建一个元素的元组:
>>> 'a',
('a',)
>>> x = 'a',
>>> type(x)
<type 'tuple'>
删除多余的逗号,这应该至少可以解决您的一个问题。