Django教程unicode不工作
我的models.py中有以下内容Django教程unicode不工作,django,python-3.x,Django,Python 3.x,我的models.py中有以下内容 import datetime from django.utils import timezone from django.db import models # Create your models here. class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published
import datetime
from django.utils import timezone
from django.db import models
# Create your models here.
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __unicode__(self):
return self.choice_text
但是当我进去的时候
from polls.models import Poll, Choice
Poll.objects.all()
我不明白
民意测验:怎么了?
但是
轮询:轮询对象
有什么想法吗?Django 1.5对Python 3提供了实验性支持,但它是为Python 2.X编写的: 本教程是为Django 1.5和Python 2.x编写的。如果Django版本不匹配,您可以参考教程了解您的Django版本,或者将Django更新为最新版本。如果您使用的是Python3.x,请注意,您的代码可能需要与教程中的内容有所不同,并且只有在知道使用Python3.x时,才应该继续使用教程 在Python3中,您应该定义一个
\uuuu str\uuuu
方法,而不是\uuuuu unicode\uuuu
方法。有一个修饰符python_2\u unicode_compatible
,它可以帮助您编写在python 2和python 3中工作的代码
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question
有关更多信息,请参阅文档中的str和unicode方法部分。我重新编辑了所有空格,每个选项卡有4个空格重新保存,重新启动shell无效。仍然不起作用我使用的python版本是否重要?我有3.2.3是的,这就解释了。这是因为您使用的是Python 3。请参阅我的更新答案。@Alasdair,在Python2.7.5和Django 1.5上有相同的错误。1@Zubair请问一个新问题,我在评论中帮不了你