I advice you to learn learn Python Language first before you start messing
with Django.

On Mon, Dec 9, 2019 at 2:19 AM Bruckner de Villiers <
[email protected]> wrote:

> 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 <[email protected]> wrote:
>
> 
> 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
>
> On Mon, Dec 9, 2019 at 9:24 AM Bruckner de Villiers <[email protected]>
> wrote:
>
>> 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
>>
>> --
>> 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 [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/VI1PR07MB5680C26D410997944FF9A20BC8580%40VI1PR07MB5680.eurprd07.prod.outlook.com
>> <https://groups.google.com/d/msgid/django-users/VI1PR07MB5680C26D410997944FF9A20BC8580%40VI1PR07MB5680.eurprd07.prod.outlook.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAHEnUVXhx5C%2BwhvAoBzOq4s_SXMxqzhq_7Y5k5fCmmhEqr%2B75Q%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAHEnUVXhx5C%2BwhvAoBzOq4s_SXMxqzhq_7Y5k5fCmmhEqr%2B75Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> --
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/07F65819-A66D-4F0B-BCBB-97EB1F1AD02C%40gmail.com
> <https://groups.google.com/d/msgid/django-users/07F65819-A66D-4F0B-BCBB-97EB1F1AD02C%40gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFgO-6hKc_ZQqXwOhUOQpq6pPNR1X47kucT_6Stw2H6517BCYQ%40mail.gmail.com.

Reply via email to