【问题标题】:Ascii Code Error in File Upload on Ubuntu Server在 Ubuntu 服务器上上传文件时出现 Ascii 代码错误
【发布时间】:2015-12-09 14:10:09
【问题描述】:

我的项目在我的本地主机中正常运行,但项目在服务器中出现 ascii 代码错误。

Django output:
    'ascii' codec can't encode character u'\u0130' in position 78: ordinal not in range(128)

这个我的视图;

def gallery(request):
    if admin_permission(request):
        return HttpResponseRedirect("/")
    showcase_list = Gallery.objects.all()
    if request.method == 'POST':
        user = User.objects.get(id=request.user.id)
        product_code = request.POST.get('product_code')
        photo = request.FILES.get('photo')
        small_photo = request.FILES.get('small_photo')
        # photo.name = 'photo'
        # small_photo.name = 'small_photo'
        code = request.POST.get('code')
        product = Products.objects.get(product_code=product_code)
        gallery = Gallery.objects.create(user=user, code=code, product=product, photo=photo, small_photo=small_photo,
                                         is_active=True)
        gallery.save()
    return render_to_response('Product/showcase/gallery.html', locals(), context_instance=RequestContext(request))

错误页面:

这是文件上传错误。如果文件名包含土耳其字符,则给出该错误。请。帮帮我……

【问题讨论】:

标签: python django file ubuntu server


【解决方案1】:

可能的原因是您的 django 进程使用 C/POSIX 语言环境运行:

$ LC_ALL=C python2
>>> import os
>>> os.listdir(u"\u0130")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0130' in position 0: ordinal not in range(128)

要修复它,请配置 utf-8 语言环境,例如 LC_ALL=C.UTF-8

【讨论】:

  • 是的,我有这个错误。我在语言环境中进行了配置,但它不起作用。
  • @KAYKISIZ:“不起作用”信息量不大。在 django 服务器中的 http 请求期间打印 LANGUAGELC_ALLLC_CTYPELANG envvars。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
相关资源
最近更新 更多