Thursday, July 25, 2013

Re: cannot import name current_datetime

On 25/07/2013 5:18pm, jacob poke wrote:
> Djangobook on the internet. Quick beginners question: In Chapter 3, where
>>I am looking at a dynamic webpage in the 2nd example I am running into some
>>problems with the current_datetime function I am trying to set up. When, in
>>views.py I insert the "datetime.datetime.now()" statement and "import
>>current_datetime" into my urls.py file, I get the following error message
>>when I try to call up the webpage via runserver. It seems like my pure
>>python script is not being picked up by the server:
>>
>>"ImportError cannot import name current_datetime".

You need to look at a Python tutorial about namespaces to understand
what is happening.

The error is saying you have tried to import something which cannot be
found. There are two solutions:

1. tell Python where to look for current_datetime

2. put the current_datetime function near the top of the file in which
you are calling it.

It is good practice to keep your main program files relatively
uncluttered and put such "utility" functions somewhere they can be
called by lots of different modules. This makes 1. above the preferred
solution.

from .date_utilities import current_datetime

... where the dot indicates that date_utilities.py is in the same
directory as the file within which you make the call. Otherwise, you
need to put date_utilities.py in directory which is on the Python path.
For example:

from utils.date_utilities import current_datetime

hth

Mike


>>
>>

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment