Unicode编码错误“ latin-1”编解码器无法在位置4939处编码字符u'\ u2013':序数不在范围(256)

时间:2018-06-28 08:51:17

标签: python django unicode encoding internationalization

我正在尝试为Django中的交易报告提供pdf文件。大部分交易工作正常。一些引发此错误 / app / finance / transaction-report /

处的UnicodeEncodeError

'latin-1'编解码器无法在位置4939处编码字符u'\ u2013':序数不在范围内(256)

这是我的pdf html

{% extends "pdf_base.html" %}
{% load i18n %}
{% load static %}

{% block content %}

<h4 style="text-align: center; text-decoration: underline; font-weight: bold; margin-bottom: 15px;">
    Transaction Report
</h4>
<table class='table-bordered'>
    <tr>
        <td>
            Type:
        </td>
        <td>
            {{instance.get_transaction_type_display|default:"-"}}
        </td>
    </tr>
    <tr>
        <td>
            Category:
        </td>
        <td>
                {% if not instance.transaction_category.is_system_generated or instance.transaction_category.name == "credit" or instance.transaction_category.name == "debit" %}
                {% if request.user.is_superuser or 'can_view_transaction_category' in user_instance.permissionlist %}
                    {{instance.transaction_category.name}}
                {% else %}
                    {{instance.transaction_category.name}}                  
                {% endif %}
            {% else %}
                {{instance.transaction_category.name}}                  
            {% endif %}
        </td>
    </tr>
    {% if category_name == "Vehicle payment" %}
    <tr>
        <td>
            Expense Type:
        </td>
        <td>
            {{instance.vehicle_expense|default:"-"}}
        </td>
    </tr>
    {%endif%}
    {% if instance.buidling_name %}
    <tr>
        <td>
            Building:
        </td>
        <td>
            {{instance.buidling_name|default:"-"}}
        </td>
    </tr>
    {%endif%}
    <tr>
        <td>
            Date:
        </td>
        <td>
            {{instance.date|default:"-"|date:"d/m/Y"}}
        </td>
    </tr>
    {% if instance.details %}
    <tr>
        <td>
            Details:
        </td>
        <td>
            {{instance.details|default:"-"}}
        </td>
    </tr>
    {% endif %}
    <tr>
        <td>
            Amount:
        </td>
        <td>
            {{instance.amount|default:"-"}}
        </td>
    </tr>
    {% if instance.amount_vat %}
    <tr>
        <td>
            Amount VAT:
        </td>
        <td>
            {{instance.amount_vat|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.vat %}
    <tr>
        <td>
            VAT:
        </td>
        <td>
            {{instance.vat|default:"-"}}
        </td>
    </tr>
    {% endif %}
    <tr>
        <td>
            Description:
        </td>
        <td>
            {{instance.description|default:"-"}}
        </td>
    </tr>
    {% if instance.Total_rent_year %}
    <tr>
        <td>
            Total Year Rent:
        </td>
        <td>
            {{instance.Total_rent_year|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.Total_rent_year_tax %}
    <tr>
        <td>
            Total Year Rent with Tax:
        </td>
        <td>
             {{instance.Total_rent_year_tax|default:"-"}}
        </td>
    </tr>
    {% endif %}

</table>
<br><br>
{% if not instance.credit %}
<b>Transaction Details</b>
<table>
    <tr>
        <td>
            Transaction Mode:
        </td>
        <td>
            {{instance.get_transaction_mode_display|default:"-"}}
        </td>
    </tr>
    {% if instance.payment_mode %}
    <tr>
        <td>
            Payment Mode:
        </td>
        <td>
            {{instance.get_payment_mode_display|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.cheque_details %}
    <tr>
        <td>
            is Cheque Withdrawed?:
        </td>
        <td>
            {{instance.is_cheque_withdrawed|yesno:"Yes,No"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.card_details %}
    <tr>
        <td>
            Card details:
        </td>
        <td>
            {{instance.card_details|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.payment_to and instance.transaction_mode == "bank" %}
    <tr>
        <td>
            Payment to:
        </td>
        <td>
            {{instance.get_payment_to_display|default:"-"}}
        </td>
    </tr>
    {% endif %}

    {% if instance.bank_account %}
    <tr>
        <td>
            Bank Account:
        </td>
        {% if request.user.is_superuser or 'can_view_bank_account' in user_instance.permissionlist %}
        <td>
            {{instance.bank_account.name|default:"-"}}
        </td>
        {% else %}
        <td>
            {{instance.bank_account.name|default:"-"}}
        </td>
        {% endif %}
    </tr>
    {% endif %}
    {% if instance.cash_account %}
    <tr>
        <td>
            Cash Account:
        </td>
        {% if request.user.is_superuser or 'can_view_cash_account' in user_instance.permissionlist %}
        <td>
            {{instance.cash_account.name|default:"-"}}
        </td>
        {% else %}
        <td>
            {{instance.cash_account.name|default:"-"}}
        </td>
        {% endif %}
    </tr>
    {% endif %}
    {% if instance.client_bank_account_details %}
    <tr>
        <td>
            Client Bank Account:
        </td>
        <td>
            {{instance.client_bank_account_details|default:"-"}}
        </td>
    </tr>
    {% endif %}
</table>
<br><br>

{% endif %}
{% if instance.credit %}
<br><br>
<b>Credit</b>
<table>
    {% if instance.credit %}
    <tr>
        <td>
            Credit:
        </td>
        <td>
            {{instance.credit|default:"-"}}
        </td>
    </tr>
    {% endif %}
</table>
<br><br>
{% endif %} 
{% if additional_info %}
<b>Additional Details</b>
<table>
    {% if instance.event %}
    <tr>
        <td>
            Event:
        </td>
        <td>
            {{instance.event|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.project %}
    <tr>
        <td>
            Project:
        </td>
        <td>
            {{instance.project|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.design %}
    <tr>
        <td>
            Design:
        </td>
        <td>
            {{instance.design|default:"-"}}
        </td>
    </tr>
    {% endif %}   
    {% if instance.client %}
    <tr>
        <td>
            Client:
        </td>
        <td>
            {{instance.client|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.supplier %}
    <tr>
        <td>
            Supplier:
        </td>
        <td>
            {{instance.supplier|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.staff %}
    <tr>
        <td>
            Staff:
        </td>
        <td>
            {{instance.staff|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.organizer %}
    <tr>
        <td>
            Organizer:
        </td>
        <td>
            {{instance.organizer|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.sub_contractor %}
    <tr>
        <td>
            Sub contractor:
        </td>
        <td>
            {{instance.sub_contractor|default:"-"}}
        </td>
    </tr>
    {% endif %}
    {% if instance.shop_credit_user%}
    <tr>
        <td>
            Credit/Debit User:
        </td>
        <td>
            {{instance.shop_credit_user|default:"-"}}
        </td>
    </tr>
    {% endif %}
</table>
<br><br>
{% endif %}

{% if instance.expenses == "general_expenses" %}
<b>General expenses</b>
<table>
        <tr>
                <td>
                    Category:
                </td>
                <td>
                    {{instance.get_general_expenses_display|default:"-"}}
                </td>
            </tr>
</table>
<br><br>
{% elif instance.expenses == "hr_Expenses"  %}
<b>HR expenses</b>
<table>
        <tr>
                <td>
                    Category:
                </td>
                <td>
                        {{instance.get_hr_expenses_display|default:"-"}}
                </td>
            </tr>
            <tr>
                    <td>
                        Staff:
                    </td>
                    <td>
                            {{instance.staff|default:"-"}}
                    </td>
                </tr>
</table>
<br><br>
{% elif instance.expenses == "utility_expenses"  %}
<b>Utility expenses</b>   
<table>
        <tr>
                <td>
                    Account Number:
                </td>
                <td>
                        {{instance.account_number|default:"-"}}
                </td>
            </tr>
            <tr>
                <td>
                    Invoice Number:
                </td>
                <td>
                        {{instance.invoice_number|default:"-"}}
                </td>
            </tr>
            <tr>
                    <td>
                        Month:
                    </td>
                    <td>
                            {{instance.month|default:"-"}}
                    </td>
                </tr>
</table>
<br><br>

{% elif instance.expenses == "project" %}
<b>Project expenses</b>
<table>
        <tr>
                <td>
                    Project:
                </td>
                <td>
                    {{instance.project|default:"-"}}
                </td>
            </tr>
</table>
<br><br>
{% endif %}
<b>Created &amp; Updated</b>
<table>
    <tr>
        <td>
            Date Added:
        </td>
        <td>
                {{instance.date_added}}
        </td>
    </tr>
    <tr>
        <td>
             Date Updated:
        </td>
        <td>
                {{instance.date_updated}}
        </td>
    </tr>
    <tr>
        <td>
            Created User:
        </td>
        <td>
                {{instance.creator.username}}
        </td>
    </tr>
    <tr>
            <td>
                Last Updated User:
            </td>
            <td>
                    {{instance.updator.username}}
            </td>
        </tr>
</table>
{% endblock %}

view.py

@check_mode
@login_required
@permissions_required(['can_view_transaction'])
def transactionreport(request):
pk= request.session.get('key')
additional_info = False
instance = get_object_or_404(
    Transaction.objects.filter(pk=pk, is_deleted=False))     
if instance.project or instance.event or instance.client or instance.supplier or instance.staff or instance.organizer or instance.contractor or instance.shop_credit_user:
    additional_info = True
category_name = instance.transaction_category.name
category_name = category_name.replace("_", " ")
category_name = category_name.capitalize()

context = {
    "instance": instance,
    "category_name": category_name,
    "title": "Transaction : " + category_name,
    "single_page": True,
    "additional_info": additional_info,
}

return render_to_pdf('finance/transaction_report.html',context)

请帮帮我

1 个答案:

答案 0 :(得分:1)

Latin-1是一个有限的字符集,只有256个字符值,因此您需要一种可以处理整个Unicode范围的编码。

UTF-8是Latin-1的Unicode超集。如果您选择UTF-8作为编码,它应该可以解决您的问题。

祝你好运@阿伦。