DoesNotExist at /admin/login/ Site matching query does not exist.

今天在使用Django.allauth的时候,进入http://127.0.0.1:8000/admin/login时遇到了
**DoesNotExist at /admin/login/ Site matching query does not exist.**错误
settings.py的改动部分如下:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myform',
    'captcha',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    # 下面是第三方账号相关的,比如我选了weibo和github
    'allauth.socialaccount.providers.weibo',

]
AUTHENTICATION_BACKENDS = (
      # django admin所使用的用户登录与django-allauth无关
      'django.contrib.auth.backends.ModelBackend',
      # `allauth` specific authentication methods, such as login by e-mail
      'allauth.account.auth_backends.AuthenticationBackend',
)
# 前面我们app里添加了django.contrib.sites,需要设置
SITE_ID = 1
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True

查询了网上资料后发现SITE_ID = 1出了问题,我之前创建了两个后台管理账户,于是我删掉了一个,成功解决。
可以先用命令python manage.py shell进入交互环境,用下面指令查询domain

>>> from django.contrib.sites.models import Site
>>> # get_current() came from SiteManager class manager, 
>>> # see site-packages/django/contrib/sites/models.py
>>> current_site = Site.objects.get_current()
>>> print(current_site.pk)
2
>>> print(current_site.domain)
example.com

你可能感兴趣的:(django)