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

Posted on

Another article related to the error generated upon executing a web-based application powered by Django framework is written in this one. The error is as shown in the title of this article which is happened when a specific command executed.

python manage.py createsuperuser

The following is the output of the error generated when the above command is executed :

root@hostname:/home/user/apps/deploy# python manage.py createsuperuser
Traceback (most recent call last):
  File "manage.py", line 10, in 
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/management/commands/createsuperuser.py", line 52, in execute
    return super(Command, self).execute(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 398, in execute
    self.check()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 23, in check_resolver
    for pattern in resolver.url_patterns:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/user/apps/deploy/deploy/urls.py", line 19, in 
    url(r'^admin/', admin.site.urls),
NameError: name 'admin' is not defined
You have new mail in /var/mail/root
root@hostname:/home/user/apps/deploy#

The exact error is actually specified above where the main error is :

NameError: name 'admin' is not defined

So, upon accessing the ‘admin’ URL which is represented by an URL, the cause of the problem obviously lays on the definition of the URL itself which cannot be solved. The above error can be solved by doing the following thing which is editing a file named ‘urls.py’ located in the project folder or directory which is normally located in the following location :

root@hostname:/home/user/apps# tree deploy/
deploy/
├── db.sqlite3
├── deploy
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── module
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── serializers.py
│   ├── serializers.pyc
│   ├── tests.py
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── logs
│   ├── localhost.django.test_web_acces.log
│   ├── localhost.django.test.web_acces.log
│   └── localhost.django.test.web_error.log
└── manage.py

4 directories, 31 files
root@hostname:/home/user/apps# 

by adding a line as follows :

from django.contrib import admin

After adding that line above, just re-execute the application.

Leave a Reply