我有一个由Django Rest Framework构成的api后端,我无法使用 code /温州银行.png 这样的中文字符返回我的图像文件。它总是返回以下错误:
文件“/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py”,第149行,在get_response中 response = self.process_exception_by_middleware(e,request)
文件“/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py”,第147行,在get_response中 response = wrapped_callback(request,* callback_args,** callback_kwargs)
文件“/usr/local/lib/python2.7/site-packages/django/views/decorators/csrf.py”,第58行,在wrapped_view中 return view_func(* args,** kwargs)
文件“/usr/local/lib/python2.7/site-packages/rest_framework/viewsets.py”,第83行,在视图中 return self.dispatch(request,* args,** kwargs)
文件“/usr/local/lib/python2.7/site-packages/rest_framework/views.py”,第477行,发送 response = self.handle_exception(exc)
在handle_exception中的文件“/usr/local/lib/python2.7/site-packages/rest_framework/views.py”,第437行 self.raise_uncaught_exception(EXC)
文件“/usr/local/lib/python2.7/site-packages/rest_framework/views.py”,第448行,在raise_uncaught_exception中 提出意见
UnicodeEncodeError:'ascii'编解码器无法对位置9-12中的字符进行编码:序数不在范围内(128)
有什么方法可以解决这个问题吗?
我的图书馆:
Django==1.9.2
djangorestframework==3.5.1
我的 serializers.py :
class RemitPayeeSerializer(serializers.ModelSerializer):
class Meta:
model = RemitPayee
fields = '__all__'
depth = 2
def to_internal_value(self, data):
print 'To Internal Value'
ret = super(RemitPayeeSerializer, self).to_internal_value(data)
if data.get('level'):
ret['level'] = \
[int(level) for level in data.get('level').split(',')]
if data.get('bank'):
ret['bank'] = data.get('bank')
return ret
def validate(self, data):
'''
'''
ret = super(RemitPayeeSerializer, self).validate(data)
request = self.context['request']
if request.method == 'POST' or request.method == 'PUT':
if ret.get('remit_type') != 1 and \
ret.get('qr_code') is not None:
form = QRCodeUploadForm(request.data, request.FILES)
if form.is_valid():
ret['qr_code'] = form.cleaned_data['qr_code']
else:
raise serializers.ValidationError({
'error': [{'QR_code': _('Invalid QR Code')}]})
return ret
@is_advpermitted('change_remitpayee')
def update(self, instance, validated_data):
'''
'''
member_levels = validated_data.pop('level', None)
bank = validated_data.get('bank')
if bank:
validated_data['bank'] = Bank.objects.get(pk=bank)
for key, val in validated_data.items():
setattr(instance, key, validated_data[key])
instance.save()
if member_levels is not None:
instance.level.clear()
for level_id in member_levels:
level = Level.objects.get(pk=level_id)
instance.level.add(level)
instance.save()
return instance
@is_advpermitted('change_remitpayee')
def create(self, data):
'''
'''
member_levels = data.pop('level', None)
bank = data.get('bank')
data['bank'] = Bank.objects.get(pk=bank) if bank else None
remit_payee = RemitPayee.objects.create(**data)
if member_levels:
for level_id in member_levels:
level = Level.objects.get(pk=level_id)
remit_payee.level.add(level)
remit_payee.save()
return remit_payee
def to_representation(self, obj):
logger.info('----start-----')
ret = super(RemitPayeeSerializer, self).to_representation(obj)
request = self.context['request']
expand = request.GET.get('opt_expand')
# user, _ = parse_request_for_token(request)
# member = Member.objects.filter(user=user).first()
# if member:
# ret['remit_limit'] = member.level.remit_limit
logger.info('----Remit ret-----')
logger.info(ret)
bank = ret.get('bank')
if bank:
if expand:
ret['bank'] = {'id': bank['id'], 'name': bank['name']}
else:
ret['bank'] = bank['id']
level = ret.get('level')
if level:
member_levels = []
for x in level:
if expand:
member_levels.append({'id': x['id'], 'name': x['name'],
'remit_limit': x['remit_limit']})
else:
member_levels.append(str(x['id']))
ret['level'] = member_levels
return ret
我的 RemitPayee模型:
class RemitPayee(models.Model):
remit_type = models.IntegerField(
default=1, null=True, blank=True, choices=REMIT_TYPE_OPTIONS)
payee_name = models.CharField(max_length=255, null=True, blank=True)
address = models.CharField(max_length=255, null=True, blank=True)
account = models.CharField(max_length=255, null=True, blank=True)
memo = models.TextField(null=True, blank=True)
nickname = models.CharField(max_length=255, null=True, blank=True)
qr_code = models.ImageField(upload_to='codes/',
null=True, blank=True)
sum_fund = models.FloatField(null=True, blank=True)
status = models.IntegerField(
default=1, null=True, blank=True, choices=STATUS_OPTIONS)
bank = models.ForeignKey(
Bank, null=True, blank=True, related_name='remit_payee_bank')
level = models.ManyToManyField(
Level, related_name='remit_payee_member_level')