python manage.py createsuperuser运行错误

在这里插入图片描述
我把思念作笺,随风而去,落在你常路过的那个街角…

错误复现

PS D:\教学文件\Django\djangoProject\webDemo02> python manage.py createsuperuser
System check identified some issues:

WARNINGS:
?: (urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
Traceback (most recent call last):
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.OperationalError: no such table: auth_user

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\教学文件\Django\djangoProject\webDemo02\manage.py", line 22, in <module>
    main()
  File "D:\教学文件\Django\djangoProject\webDemo02\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 88, in execute
    return super().execute(*args, **options)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\core\management\base.py", line 458, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 109, in handle
    default_username = get_default_username(database=database)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\contrib\auth\management\__init__.py", line 168, in get_default_username
    auth_app.User._default_manager.db_manager(database).get(
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 633, in get
    num = len(clone)
          ^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 380, in __len__
    self._fetch_all()
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 1881, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\query.py", line 91, in __iter__
    results = compiler.execute_sql(
              ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\models\sql\compiler.py", line 1562, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 102, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    with self.db.wrap_database_errors:
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\ASUS\AppData\Local\Programs\Python\Python311\Lib\site-packages\django\db\backends\sqlite3\base.py", line 328, in execute
    return super().execute(query, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
django.db.utils.OperationalError: no such table: auth_user

错误解析

这个错误消息表明Django在执行python manage.py createsuperuser命令时无法找到auth_user表,通常是因为数据库尚未初始化或者数据库迁移(migrations)尚未完成。要解决这个问题,您可以按照以下步骤操作:

  1. 确保您已经在项目目录中运行了以下命令以应用数据库迁移:

    python manage.py makemigrations
    python manage.py migrate
    

    这将创建数据库表并将其初始化,包括auth_user表。

  2. 如果您之前已经执行了上述命令,但仍然遇到问题,可能是数据库出现了一些问题。您可以尝试删除数据库并重新创建它,然后再次运行迁移命令:

    rm db.sqlite3  # 删除数据库文件
    python manage.py makemigrations
    python manage.py migrate
    
  3. 如果您在项目中使用的是不同的数据库引擎(例如MySQL或PostgreSQL),请确保数据库服务器正在运行,并且数据库配置正确。

  4. 如果您使用的是SQLite数据库,确保您有写入数据库文件的权限,因为有时权限问题可能导致无法创建数据库表。

按照这些步骤之一,您应该能够成功执行python manage.py createsuperuser命令并创建超级用户。

在这里插入图片描述

python manage.py createsuperuser Traceback (most recent call last): File "E:\learn\ttsxzjc\manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line utility.execute() File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\core\management\__init__.py", line 347, in execute django.setup() File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\__init__.py", line 16, in setup from django.urls import set_script_prefix File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\urls\__init__.py", line 1, in <module> from .base import ( File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\urls\base.py", line 8, in <module> from .exceptions import NoReverseMatch, Resolver404 File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\urls\exceptions.py", line 1, in <module> from django.http import Http404 File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\http\__init__.py", line 5, in <module> from django.http.response import ( File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\http\response.py", line 13, in <module> from django.core.serializers.json import DjangoJSONEncoder File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\core\serializers\__init__.py", line 23, in <module> from django.core.serializers.base import SerializerDoesNotExist File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\core\serializers\base.py", line 6, in <module> from django.db import models File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\db\models\__init__.py", line 3, in <module> from django.db.models.aggregates import * # NOQA ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\learn\ttsxzjc\.venv\Lib\site-packages\django\db\models\aggregates.py", line 5, in <module> from django.db.models.expressions import Case, Func, Star, When File "E:\learn\ttsxzjc\.venv\Lib\site-packages\djan
03-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT小辉同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值