How to Solve Error Message NameError: name ‘timezone’ is not defined

Posted on

This article will show how to solve the problem arise upon execution of a certain command when developing Django web-based application. It exst when executing the following command :

python manage.py makemigrations

It is a command to migrate or to implement the model into the database specifically its own associated table. But unfortunately, after executing the command, there is an error message appear as follows :

(myenv) C:\python-win\project>python manage.py makemigrations
Traceback (most recent call last):
  File "manage.py", line 21, in 
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\python-win\myenv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\python-win\myenv\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    django.setup()
  File "C:\python-win\myenv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\python-win\myenv\lib\site-packages\django\apps\registry.py", line 114, in populate
    app_config.import_models()
  File "C:\python-win\myenv\lib\site-packages\django\apps\config.py", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "c:\python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "", line 1014, in _gcd_import
  File "", line 991, in _find_and_load
  File "", line 975, in _find_and_load_unlocked
  File "", line 671, in _load_unlocked
  File "", line 783, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "C:\python-win\project\app\models.py", line 5, in 
    class Pangkat(models.Model):
  File "C:\python-win\project\app\models.py", line 10, in Employee
    date_created = models.DateTimeField(default=timezone.now)
NameError: name 'timezone' is not defined

The error message appear in the last line of the output message as it shows above. That last line is in the following :

NameError: name 'timezone' is not defined

The cause of the error is actually exist in the line of code definition in the models.py file. The declaration of a model with a certain attribute triggering the error message. That declaration is in the following line of source code :

class Employee(models.Model):
    namea = models.CharField(max_length=50)
    date_created = models.DateTimeField(default=timezone.now)
    
    def __str__(self):
        return "%s Employee : %s" % (self.name)

The attribute using parameter ‘default=timezone.now’ will generate the error. It is because Django itself after rendering the source code cannot recognize the term of ‘timezone’. So, the solution is actually quite simple. Just add the definition of ‘timezone’ by importing it to the source code. Fortunately, the ‘timezone’ has an already built in the Django framework package, module or utility. Just add the import line as follows in the top or the beginning of the source code :

from django.utils import timezone

So, the source code will be in the following layout :

from django.utils import timezone

class Employee(models.Model):
    namea = models.CharField(max_length=50)
    date_created = models.DateTimeField(default=timezone.now)
    
    def __str__(self):
        return "%s Employee : %s" % (self.name)

Try to execute the command again, the following output will appear :

(myenv) C:\python-win\project>python manage.py makemigrations
Migrations for 'app':
  app\migrations\0002_auto_20200517_1113.py
    - Add field date_created to employee

(myenv) C:\python-win\project>

Finally, the execution of the above command is a success. The new attribute using the ‘timezone’ parameter has already been added successfully.

Leave a Reply