You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django报错:django_site表不存在,请求排查解决

解决Django中django_site表不存在的错误

执行Site.objects.count()时触发如下错误:

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

完整报错栈信息:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\models\query.py", line 621, in count
    return self.query.get_count(using=self.db)
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\models\sql\query.py", line 559, in get_count
    return obj.get_aggregation(using, ["__count"])["__count"]
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\models\sql\query.py", line 544, in get_aggregation
    result = compiler.execute_sql(SINGLE)
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\models\sql\compiler.py", line 1398, in execute_sql
    cursor.execute(sql, params)
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\backends\utils.py", line 102, in execute
    return super().execute(sql, params)
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\utils.py", line 91, in __exit__    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "E:\Django Unleashed\django-unleashed\lib\site-packages\django\db\backends\sqlite3\base.py", line 357, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: django_site

提供的django.contrib.sites中Site类代码:

class Site(models.Model):
    domain = models.CharField(
        _("domain name"),
        max_length=100,
        validators=[_simple_domain_name_validator],
        unique=True,
    )
    name = models.CharField(_("display name"), max_length=50)

    objects = SiteManager()

    class Meta:
        db_table = "django_site"
        verbose_name = _("site")
        verbose_name_plural = _("sites")
        ordering = ["domain"]

    def __str__(self):
        return self.domain

    def natural_key(self):
        return (self.domain,)

错误原因

这个错误的核心是你的数据库中不存在django_site表。django.contrib.sites是Django的内置应用,用于多站点管理,它的数据库表需要通过迁移命令生成,且必须先在项目配置中注册该应用。

解决方法

1. 确认应用已注册并配置SITE_ID

打开项目根目录下的settings.py,确保django.contrib.sites在INSTALLED_APPS列表中,同时添加SITE_ID配置:

INSTALLED_APPS = [
    # 其他已注册的应用
    'django.contrib.sites',  # 确保这一行存在
]

# 添加SITE_ID配置,默认设为1即可
SITE_ID = 1

2. 执行数据库迁移命令

在项目根目录的终端中运行以下命令,生成并应用django.contrib.sites的数据库迁移:

# 生成迁移文件(内置应用的迁移已存在,此步可跳过,但执行也无影响)
python manage.py makemigrations

# 应用所有迁移,创建django_site表
python manage.py migrate

执行migrate后,Django会自动在数据库中创建django_site表,并插入一条默认站点记录(domain为example.com,name为example.com)。

3. 验证修复结果

运行Django shell验证:

python manage.py shell

在shell中执行:

from django.contrib.sites.models import Site
print(Site.objects.count())  # 应输出1,代表表已创建且有默认记录

额外排查点

  • 如果执行迁移后仍报错,检查数据库连接配置是否正确,确保操作的是目标数据库。
  • 若数据库已存在旧数据,可尝试备份后清空数据库,重新执行迁移命令。

内容的提问来源于stack exchange,提问作者user21738139

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 23:30:37