Django多站点:ValueError:尚未设置SITE_ID

时间:2015-09-04 00:39:21

标签: python django

我最近有了一台新计算机并且一直在尝试重新设置我的开发环境,现在我需要开发一个Django项目。但是,每当我尝试python manage.py runservermakemigrationsmigrate等时,我都会遇到以下错误,并且我不明白为什么。

Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
    django.setup()
  File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/usr/local/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/usr/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 232, in patch_all
    patch_root_urlconf()
  File "/usr/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 220, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 550, in reverse
    app_list = resolver.app_dict[ns]
  File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 352, in app_dict
    self._populate()
  File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 285, in _populate
    for pattern in reversed(self.url_patterns):
  File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 402, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 396, in urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/Lily/Sites/StartupTree/StartupTree/urls.py", line 48, in <module>
    + static(settings.STATIC_URL, document_root=settings.STATICFILES_DIRS[0])
  File "/usr/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 33, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/Lily/Sites/StartupTree/branch/urls.py", line 2, in <module>
    from branch.views import (
  File "/Users/Lily/Sites/StartupTree/branch/views/__init__.py", line 15, in <module>
    from app.views import home as home_redirect
  File "/Users/Lily/Sites/StartupTree/app/views.py", line 5, in <module>
    from emailuser.views import user_login as login_redirect
  File "/Users/Lily/Sites/StartupTree/emailuser/views/__init__.py", line 32, in <module>
    from branch.modelforms import (
  File "/Users/Lily/Sites/StartupTree/branch/modelforms.py", line 139, in <module>
    class ExperienceForm(forms.ModelForm):
  File "/usr/local/lib/python2.7/site-packages/django/forms/models.py", line 285, in __new__
    opts.help_texts, opts.error_messages)
  File "/usr/local/lib/python2.7/site-packages/django/forms/models.py", line 212, in fields_for_model
    formfield = f.formfield(**kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1981, in formfield
    'queryset': self.rel.to._default_manager.using(db),
  File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/sites/managers.py", line 66, in get_queryset
    **{self._get_field_name() + '__id': settings.SITE_ID})
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 679, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 697, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1309, in add_q
    clause, require_inner = self._add_q(where_part, self.used_aliases)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1337, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1208, in build_filter
    condition = self.build_lookup(lookups, col, value)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1101, in build_lookup
    return final_lookup(lhs, rhs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/lookups.py", line 101, in __init__
    self.rhs = self.get_prep_lookup()
  File "/usr/local/lib/python2.7/site-packages/django/db/models/lookups.py", line 139, in get_prep_lookup
    return self.lhs.output_field.get_prep_lookup(self.lookup_name, self.rhs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 727, in get_prep_lookup
    return self.get_prep_value(value)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 985, in get_prep_value
    return int(value)
  File "/usr/local/lib/python2.7/site-packages/multisite/threadlocals.py", line 57, in __int__
    return self.get_default()
  File "/usr/local/lib/python2.7/site-packages/multisite/threadlocals.py", line 119, in get_default
    raise ValueError('SITE_ID has not been set.')
ValueError: SITE_ID has not been set.

我已经检查了我的settings.py并找到SITE_ID = 1所以我假设它想要的是什么? (我也是Django的新手,抱歉!)

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

使用django-multisite时,您需要按以下方式设置SITE_ID

from multisite import SiteID
SITE_ID = SiteID()

答案 1 :(得分:0)

从命令行介绍您的环境:

$ export SITE_ID = 1