2015-09-04 72 views
0

我最近得到了一臺新電腦,並一直試圖將我的開發環境備份起來,現在我需要開發一個Django項目。但是,每當我嘗試執行python manage.py runservermakemigrationsmigrate等等時,我都會遇到以下錯誤,我不明白爲什麼。Django多站點:ValueError:SITE_ID尚未設置

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也很新,很抱歉!)

非常感謝您的幫助!

回答

0

當使用django-multisite需要建立SITE_ID如下

from multisite import SiteID 
SITE_ID = SiteID() 
+0

嘿安德魯,非常感謝您的建議!不幸的是,即使在settings.py中包含該片段並在django-multisite「快速入門」下仔細檢查了所有內容,仍然會發生同樣的錯誤......我假設代碼本身沒問題,但因爲多站點工作正常我的舊筆記本 – Lily

0

從命令行介紹您的環境:

$ export SITE_ID = 1