【问题标题】:'latin-1' codec can't encode characters in position 51732-51735: ordinal not in range(256) Django'latin-1' 编解码器无法对位置 51732-51735 中的字符进行编码:序数不在范围内(256)Django
【发布时间】:2020-08-13 07:58:10
【问题描述】:

我有这个错误的问题'latin-1' codec can't encode characters in position 51732-51735: ordinal not in range(256) 我该如何解决这个问题?

这是我的views.py代码

def render_to_pdf(template_src, context_dict={}):
    template = get_template(template_src)
    html  = template.render(context_dict)
    result = io.StringIO()
    pdf = pisa.pisaDocument(io.StringIO(html.encode("ISO-8859-1")), result)
    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')
    return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))    
def myview(request, pk):
    postingan_list = Pengajuan_SKTM.objects.filter(id=pk)
    return render_to_pdf(
            'pengurusan/pdf_template.html',
            {
                 'pagesize':'A4',
                 'postingan_list' : postingan_list,
            })

这是我的 urls.py 代码

url(r'^(?P<pk>[0-9]+)/myview/$', views.myview, name='print')

【问题讨论】:

  • 为什么使用 latin-1?
  • 只要关注这个来源link

标签: python django encode


【解决方案1】:

您可能尝试渲染一些 Unicode 字符,这些字符在 ISO-8859-1 编码中不存在。如果底层库支持(现在所有都应该),请使用 UTF-8,因为它支持所有字符。

【讨论】:

    猜你喜欢
    • 2015-02-13
    • 1970-01-01
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多