Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django error: cannot import name current_datetime

I'm Working through "The Django Book" and I keep getting the error "cannot import name current_datetime"

Urls.py:

from django.conf.urls.defaults import patterns, include, url
from mysite.views import current_datetime, hello

urlpatterns = patterns('', 
    ('^hello/$', hello), 
    ('^time/$', current_datetime),
    (r'^time/plus/(\d{1,2})/$', hours_ahead),
)

My Views.py:

from django.http import HttpResponse
import datetime

def hello(request):
    return HttpResponse("Hello world")

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

My working directory:

./mysite:
__init__.py manage.py   mysite      views.py

No matter what I do, I get the same import error in urls.py line 2, regarding current_time:

Environment:


Request Method: GET
Request URL: http://localhost:8000/

Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/Library/Python/2.7/site-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/Users/jvieitez/Code/djcode/mysite/mysite/urls.py" in <module>
  2. from mysite.views import hello, current_datetime, hours_ahead

Exception Type: ImportError at /
Exception Value: cannot import name current_datetime
like image 781
Jvieitez Avatar asked Apr 11 '13 21:04

Jvieitez


1 Answers

Something is wrong with your working directory. manage.py and views.py should not be in the same directory. I would recommend renaming the inner mysite to something else, so you avoid the confusion, and views.py should be in the inner mysite directory. You said

from mysite.views import current_datetime, hello

but views.py isn't in the mysite directory. That's the problem.

like image 161
mattg Avatar answered Sep 26 '22 20:09

mattg