Django集成Google认证时出现Site.DoesNotExist错误求助
解决Django allauth Google认证中的Site.DoesNotExist错误
错误根源
该错误是由于Django的sites框架未找到对应的Site实例,而allauth第三方登录依赖Site模型来关联应用配置导致的。
修复步骤
1. 确认sites应用已启用
打开项目settings.py,检查INSTALLED_APPS列表,确保包含django.contrib.sites:
INSTALLED_APPS = [ # 其他已安装应用 'django.contrib.sites', 'allauth', 'allauth.account', 'allauth.socialaccount', 'allauth.socialaccount.providers.google', ]
2. 配置SITE_ID
在settings.py中添加或指定SITE_ID:
SITE_ID = 1
3. 创建Site数据库记录
执行Django迁移命令,生成默认Site实例:
python manage.py migrate
若迁移后仍无Site记录,可通过Django Shell手动创建:
python manage.py shell
在Shell中运行以下代码(根据实际域名修改参数):
from django.contrib.sites.models import Site Site.objects.create(domain='localhost:8000', name='localhost:8000')
4. 关联Google应用与Site
登录Django后台管理界面,找到Social Accounts下的Social Apps,编辑你的Google认证应用配置,在Sites选项中勾选已创建的Site实例。
验证操作
重启Django开发服务器,访问首页,该错误应不再出现。
内容的提问来源于stack exchange,提问作者Rahul Gupta
相关产品推荐
相关产品推荐

