小编Sve*_*nze的帖子

使用Django和format-strings的UnicodeDecodeError

我为每个人写了一个小问题的例子,看看使用Python 2.7和Django 1.10.8会发生什么

# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals, print_function

import time
from django import setup
setup()
from django.contrib.auth.models import Group

group = Group(name='schön')

print(type(repr(group)))
print(type(str(group)))
print(type(unicode(group)))

print(group)
print(repr(group))
print(str(group))
print(unicode(group))

time.sleep(1.0)
print('%s' % group)
print('%r' % group)   # fails
print('%s' % [group]) # fails
print('%r' % [group]) # fails
Run Code Online (Sandbox Code Playgroud)

退出时带有以下输出+回溯

$ python .PyCharmCE2017.2/config/scratches/scratch.py
<type 'str'>
<type 'str'>
<type 'unicode'>
schön
<Group: schön>
schön
schön
schön
Traceback (most recent call last):
  File "/home/srkunze/.PyCharmCE2017.2/config/scratches/scratch.py", line 22, …
Run Code Online (Sandbox Code Playgroud)

python django format-string

10
推荐指数
1
解决办法
687
查看次数

标签 统计

django ×1

format-string ×1

python ×1