Python Flaskアプリケーションを2.7から3.5に変換しようとしています。私はSQLAlchemyを使ってシンプルなCMSシステムを持っています。私のページは、インデックスページにタイトルと内容(切り詰められています)で表示されます。それが私の問題が起こっているところです。Python 3.5 TypeError:truncateフィルタを使用して 'str'ではなくバイト型オブジェクトが必要です
私のページをリストとして、コンテンツを文字列として返します。私は "ページ"をエンコードしようとしましたが、リストオブジェクトでは許可されていないため、別のエラーが返されます。
エラー:
File "C:\Python35\lib\site-packages\jinja2\filters.py", line 481, in do_truncate
result = s[:length - len(end)].rsplit(' ', 1)[0]
TypeError: a bytes-like object is required, not 'str'
経路:
@app.route("/")
def main():
pages = db.session.query(Pages).all()
return render_template('index.html', title='Dashboard', pages=pages)
index.htmlページ
{% for page in pages %}
<div>
<a href="{{ url_for('view_page', page_id=page.id) }}"><h3>{{ page.title|truncate(150) }}</h3></a>
<p>{{ page.content|truncate(450)|striptags }}</p>
</div>
{% endfor %}
したがって、 'page.title'はバイトオブジェクトです。 'truncate'フィルタは' str'オブジェクトに対してのみ機能します。 –