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 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 makemigrations
Traceback (most recent call last):
  File "", line 21, in 
  File "", line 17, in main
  File "C:\python-win\myenv\lib\site-packages\django\core\management\", line 401, in execute_from_command_line
  File "C:\python-win\myenv\lib\site-packages\django\core\management\", line 377, in execute
  File "C:\python-win\myenv\lib\site-packages\django\", line 24, in setup
  File "C:\python-win\myenv\lib\site-packages\django\apps\", line 114, in populate
  File "C:\python-win\myenv\lib\site-packages\django\apps\", line 211, in import_models
    self.models_module = import_module(models_module_name)
  File "c:\python38\lib\importlib\", 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\", line 5, in 
    class Pangkat(models.Model):
  File "C:\python-win\project\app\", line 10, in Employee
    date_created = models.DateTimeField(
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 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(
    def __str__(self):
        return "%s Employee : %s" % (

The attribute using parameter ‘’ 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(
    def __str__(self):
        return "%s Employee : %s" % (

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

(myenv) C:\python-win\project>python makemigrations
Migrations for 'app':
    - 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