5

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
6
  • just about to say the same thing, indentation is not correct Commented Apr 11, 2013 at 21:09
  • Would you mind telling me what it should look like? I am copying it straight from djangobook.com/en/2.0/chapter03.html Thank you! Commented Apr 11, 2013 at 21:42
  • What is mysite in the folder mysite? Is it also a folder? If so, does it contain __init__.py? Commented Apr 11, 2013 at 21:48
  • @nymk yes, I recently copied __init__.py to both folders per some other thread's suggestion. Commented Apr 11, 2013 at 21:58
  • 2
    The outer mysite package was shadowed by the inner mysite. The mysite in from mysite.views import current_datetime, hello was resolved to the inner one. Commented Apr 11, 2013 at 22:08

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Bam! Solved! That was the problem. Thanks so much! I was editing the views.py located in the outer mysite folder rather than the other mysite contained in that one. I will definitely have to rename them. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.