Running Django 3.0 & Python 3.7.3
Everything works per the tutorial until I get to:
q.was_published_recently()
It throws the following error:
File "/Volumes/Data/DevelopmentTraining/django_tutorials/mysite/polls/models.py", line 19, in was_published_recently
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
I also tried importing timedelta from datetime – to no avail.
models.py code below:
import datetime
from datetime import datetime, timedelta
from django.db import \
models
from django.utils import timezone
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
Much obliged,
Bruckner de Villiers
def was_published_recently(self):
return self.pub_date >= timezone.now() - timedelta(days=1)
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/VI1PR07MB5680C26D410997944FF9A20BC8580%40VI1PR07MB5680.eurprd07.prod.outlook.com.
--
On 09 Dec 2019, at 10:38, Daniel Hepper <daniel...@gmail.com> wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHEnUVXhx5C%2BwhvAoBzOq4s_SXMxqzhq_7Y5k5fCmmhEqr%2B75Q%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/07F65819-A66D-4F0B-BCBB-97EB1F1AD02C%40gmail.com.