I'm new to Python and especially to Django. While trying to dive in subj. framework, and run through its official tutorial I got some pain in the neck error which says:
我是Python的新手,尤其是Django的新手。在尝试潜水的时候。框架,和运行它的官方教程,我在脖子上的错误是说:
Attribute error: 'Poll' object has no attribute 'was_published_recently'
属性错误:'Poll'对象没有属性'was_published_recently'
I type the next in django shell (invoked by: "python manage.py shell" from projects directory):
我在django shell中输入下一个(由:“python管理”调用)。来自项目目录的py shell:
>>> from polls.models import Poll, Choice
>>> from django.utils import timezone
>>> p = Poll.objects.get(pk=1)
>>> p.was_published_recently()从民意调查> > >。模型导入轮询,选择django的>>>。导入时区>>> p = Poll.objects.get(pk=1) >>> p.was_published_recent ()
and I get the next shell output:
得到下一个壳层输出:
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'Poll' object has no attribute 'was_published_recently'"Traceback(最近一次调用):File“”,第1行,AttributeError:“Poll”对象没有属性“was_published_recently”
Can someone help me to get what am I doing wrong here? Because I just have no idea what can lead to such an error... (Already googled inside out the question, but didn't find an answer that could solve my situation).
有人能帮我弄明白我在这里做错了什么吗?因为我不知道什么会导致这样的错误……(已经在谷歌上彻底搜索了这个问题,但没有找到能解决我的问题的答案)。
I use:
Django version 1.5.1
Python version 2.7.5
我使用:Django版本1.5.1 Python版本2.7.5
Here're my "Poll" model code:
下面是我的“投票”模型代码:
import datetime
from django.utils import timezone
from django.db import models
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
Also, here's my "Admin" file:
另外,这是我的“Admin”文件:
from django.contrib import admin
from polls.models import Choice, Poll
class ChoiceInline(admin.TabularInline):
model = Choice
extra = 3
class PollAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
list_display = ('question', 'pub_date', 'was_published_recently')
admin.site.register(Choice)
admin.site.register(Poll, PollAdmin)
2 个解决方案
#1
2
make sure you use 4 spaces as indentation instead Tab character, tab makes function does not recognize.
请确保您使用4个空格作为缩进,而不是制表符,制表符使函数无法识别。
#2
0
I think that all it is saying is that you haven't included any was_published_recently function in the class. Thanks for including the admin.py & polls.py files, but I think it's in your models.py file that you need to ensure a couple things. It looks like you need to make sure that
我认为它的意思是您没有在类中包含任何was_published_recent函数。谢谢你的邀请。py和民意调查。py文件,但是我想应该在你们的模型里。需要确保一些东西的py文件。看起来你需要确认一下。
from django.utils import timezone
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
are included in your models.py file.
包括在你的模型中。py文件。
#1
2
make sure you use 4 spaces as indentation instead Tab character, tab makes function does not recognize.
请确保您使用4个空格作为缩进,而不是制表符,制表符使函数无法识别。
#2
0
I think that all it is saying is that you haven't included any was_published_recently function in the class. Thanks for including the admin.py & polls.py files, but I think it's in your models.py file that you need to ensure a couple things. It looks like you need to make sure that
我认为它的意思是您没有在类中包含任何was_published_recent函数。谢谢你的邀请。py和民意调查。py文件,但是我想应该在你们的模型里。需要确保一些东西的py文件。看起来你需要确认一下。
from django.utils import timezone
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
are included in your models.py file.
包括在你的模型中。py文件。