Hitendra Kashyap
Hitendra Kashyap

Reputation: 11

django.db.utils.OperationalError: no such table: django_site

I am getting error when I am running python manage.py syncdb in django 1.7

I am using Python2.7 but I get the exact same error in Python 3.4.3

$ python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 13, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/debug_toolbar/settings.py", line 232, in patch_all
    patch_root_urlconf()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/debug_toolbar/settings.py", line 220, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/core/urlresolvers.py", line 522, in reverse
    app_list = resolver.app_dict[ns]
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/core/urlresolvers.py", line 329, in app_dict
    self._populate()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/core/urlresolvers.py", line 269, in _populate
    for pattern in reversed(self.url_patterns):
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/core/urlresolvers.py", line 372, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/core/urlresolvers.py", line 366, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/Users/HK/anaconda/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/HK/website/mor36/src/mor36/urls.py", line 13, in <module>
    url(r'^admin/', include(admin.site.urls)),
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 262, in urls
    return self.get_urls(), self.app_name, self.name
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 246, in get_urls
    url(r'^%s/%s/' % (model._meta.app_label, model._meta.model_name), include(model_admin.urls))
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/contrib/admin/options.py", line 598, in urls
    return self.get_urls()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/cms/admin/pageadmin.py", line 139, in get_urls
    url_patterns += super(PageAdmin, self).get_urls()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/cms/admin/placeholderadmin.py", line 121, in get_urls
    from cms.urls import SLUG_REGEXP
  File "/Users/HK/anaconda/lib/python2.7/site-packages/cms/urls.py", line 20, in <module>
    urlpatterns = get_app_patterns()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/cms/appresolver.py", line 196, in get_app_patterns
    current_site = Site.objects.get_current()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/contrib/sites/models.py", line 54, in get_current
    current_site = self.get(pk=sid)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 351, in get
    num = len(clone)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 122, in __len__
    self._fetch_all()
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/models/query.py", line 265, in iterator
    for row in compiler.results_iter():
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 701, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 787, in execute_sql
    cursor.execute(sql, params)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/Users/HK/anaconda/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
    return Database.Cursor.execute(self, query, params)
**django.db.utils.OperationalError: no such table: django_site**

Upvotes: 0

Views: 11511

Answers (2)

Kirill Kniazev
Kirill Kniazev

Reputation: 91

I can see you are using django-CMS, I've encountered the same issue. I can assume that there might be an issue with newer versions of Django.

The root cause is that Django tables are not created yet, but cms tries to refer to them (in particular to get the current site from Sites framework)

That wasn't the issue in previous versions of Django because you would usually do python manage.py sycndb (which is removed at least in Django 1.9) and that command created all the django tables.

So to solve this - you need to comment out everything related to django CMS in your settings.py, run python manage.py migrate to migrate Django tables, and then uncomment cms stuff and migrate once again, then it shouldn't be an issue anymore. (sometimes it helps to also comment current project from installed apps as well as cms stuff so that Django 1.9 checks wont check your urls.py which has entries related to cms).

If you encounter the same issue on Django <1.9 but > 1.7 try to do syncdb first.

Hope that helps to anyone who encounters similar issue.

Upvotes: 3

justcompile
justcompile

Reputation: 3542

If it doesn't already exist, add the following to the INSTALLED_APPS property in settings.py:

'django.contrib.sites'

and also add SITE_ID = 1 in settings.py as well.

In Django 1.7+, the preferred way of making DB changes is with the migrate management command:

./manage.py migrate

That should solve your problem.

Upvotes: 0

Related Questions