Difficulty with Tutorial Part 2

345 views
Skip to first unread message

Bruckner de Villiers

unread,
Dec 9, 2019, 3:24:35 AM12/9/19
to django...@googlegroups.com

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

+27 (0)83 625 1086

Daniel Hepper

unread,
Dec 9, 2019, 3:38:47 AM12/9/19
to django...@googlegroups.com
You can fix your code by changing datetime.timedelta to timedelta:

    def was_published_recently(self):

        return self.pub_date >= timezone.now() - timedelta(days=1)


The statement "from datetime import datetime, timedelta" imports the classes datetime and timedelta from the module datetime.

If you prefer to use "datetime.timedelta", you would have to use "import datetime" instead of "from datetime import datetime, timedelta"

Hope that helps,
Daniel
--
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.

Sencer Hamarat

unread,
Dec 9, 2019, 3:38:50 AM12/9/19
to django...@googlegroups.com
Your timedelta import is correct but usage of the timedelta is not.

Replace "datetime.timedelta(days=1)" with "timedelta(days=1)"


Saygılarımla,
Sencer HAMARAT



--

Bruckner de Villiers

unread,
Dec 9, 2019, 5:20:02 AM12/9/19
to django...@googlegroups.com
Thank you Daniel. I believe that issue started with VSCode automatically inserting “from date time import date time” after I hit enter on “import date time”.

Thank you for the quick response. 

Bruckner
0836251086

On 09 Dec 2019, at 10:38, Daniel Hepper <daniel...@gmail.com> wrote:



Tafadzwa Marshal

unread,
Dec 9, 2019, 8:23:45 AM12/9/19
to django...@googlegroups.com
I advice you to learn learn Python Language first before you start messing with Django.

Reply all
Reply to author
Forward
0 new messages