Problems with code in shell (Django 2 and python 3.6)

62 views
Skip to first unread message

User1

unread,
Oct 12, 2018, 5:08:06 PM10/12/18
to Django users
Hello, I am practicing the tutorial part 5, Test and I got an error I can solve.
The code in shell is:

(mientorno) C:\Users\user1\djangowom>python manage.py shell
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
 on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
>>>
>>>
>>> import datetime
>>> from django.utils import timezone
>>> from polls.models import Question
>>> future_question = Question(pub_date=timezone.now() + datetime.timedelta(days=30))
>>> future_question.was_published_recently()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\arojas\djangowom\polls\models.py", line 12, in was_published_re
cently
    return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
>>>

Any idea? Thanks a lot.

Stefan Bethke

unread,
Oct 12, 2018, 6:56:23 PM10/12/18
to django...@googlegroups.com
You're missing an additional datetime. Either:
>>> from datetime import datetime

or
>>> future_question = Question(pub_date=timezone.now() + datetime.datetime.timedelta(days=30))

When I started with Python, that confused the heck out of me: The datetime *module* has a datetime *class* in it. The datetime class really should be called DateTime, but it's been around too long to change it now, I guess.

HTH,
Stefan

-- 
Stefan Bethke <s...@lassitu.de>   Fon +49 151 14070811

Luis Enrique Gonzalez D

unread,
Oct 13, 2018, 9:39:50 AM10/13/18
to django...@googlegroups.com
It's possible that exist and confusion using libraries due to the versions of python. Try this in the import :
from datetime import datetime

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6fe58b1b-f6d5-41b6-9193-ff00d938420f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages