django/python中表单post的编码字符串
我正在尝试将CSV字符串导入到django中的模型中,它不断抛出django/python中表单post的编码字符串,python,django,Python,Django,我正在尝试将CSV字符串导入到django中的模型中,它不断抛出 'ascii' codec can't encode character u'\xe9' in position 29: ordinal not in range(128) 这是我当前的视图代码 def saveLoot(request): if 'q' in request.POST: scsv = request.POST['q'] f = StringIO.StringIO(scsv
'ascii' codec can't encode character u'\xe9' in position 29: ordinal not in range(128)
这是我当前的视图代码
def saveLoot(request):
if 'q' in request.POST:
scsv = request.POST['q']
f = StringIO.StringIO(scsv)
reader = csv.reader(f, delimiter=',')
for row in reader:
user = UserProfile.objects.get(character=row[3])
loot = Loot(loot_id=row[0], item_name=row[1], source=row[2], winner=user, loot_type=row[4], lockout_tier=row[5], time=row[6], date=row[7])
loot.save()
message = "I like pie, it's good, and guess what. I've saved that for you."
else:
message = 'Fill the damn box in will you.'
return HttpResponse(message)
这似乎是我试图从UserProfile模型中提取用户的地方,带有重音符号的字符编码不正确,导致了这个错误
我知道这可能是非常基本的,我已经尝试设置默认编码,自从我上次研究python(将近12个月)以来已经有一段时间了 我现在记得怎么做了 我当然不确定这是否是最有效的方法,但以下是我如何应对的
scsv = request.POST['q'].encode('utf-8')
如果有人有更好的方法,我会很感激的