繁体   English   中英

由于不在 TTY 中工作,无法在 django 中创建超级用户

[英]Unable to create superuser in django due to not working in TTY

我浏览了 djangoproject.com 的第一个 django 教程,在第 2 部分的开头,当我运行"python manage.py createsuperuser"时创建超级用户,我收到以下消息:

Superuser creation skipped due to not running in a TTY. You can run `manage.py createsuperuser` in your project to create one manually.    

在运行syncdb后继续创建超级用户时,我收到相同的消息。

我正在为 Windows 7 和 Django 1.7.1 以及 Python 2.7.8 开发 Eclipse。

使用 Git Bash 并纠正上述错误消息时,尝试附加winpty
即例如:

$ winpty python manage.py createsuperuser
Username (leave blank to use '...'):

如果您在 virtualenv 中,请cd进入您的 virtualenv 并激活它。 然后尝试以下步骤:

python manage.py syncdb --noinput
python manage.py migrate
python manage.py createsuperuser

您可以使用 django shell ( python manage.py shell ) 创建超级用户

from django.contrib.auth.models import User
User.objects.create_superuser(username='YourUsername', password='hunter2', email='your@email.com')

我是 Windows10 用户。 我尝试使用Git Bash控制台运行py manage.py createsuperuser命令,但已抛出错误。 然后我将Git Bash切换到具有管理员权限的本机Windows Command Line ,并重新运行命令 - 它正在工作。

使用“Windows PowerShell”或“Windows Cmd”,然后使用相同的命令。 Git 命令接口有一些限制。

第一次运行

$ django-admin startproject mysite 在 cmd 提示符下,然后通过以下方式应用迁移

cd mysite mysite:

python manage.py makemigrations 然后

之后 python manage.py migrate

python manage.py createsuperuser

Django 3.0 开始,您无需 TTY 即可完成

    DJANGO_SUPERUSER_USERNAME=admin DJANGO_SUPERUSER_PASSWORD=psw \
    python manage.py createsuperuser --email=admin@admin.com --noinput

此外,您可以将 DJANGO_SUPERUSER_PASSWORD 设置为环境变量

使用这个命令:

python3 manage.py makemigrations

python3 manage.py migrate

python3 manage.py createsuperuser

python manage.py runserver

你的错误可能是:

[Error `You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, content types, sessions.
Run 'python manage.py migrate' to apply them.

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 303, in execute
    return Database.Cursor.execute(self, query, params)`][1]

使用 Tree 命令检查您的 yo 目录: tree

然后运行 ​​Make migration :在此处输入图像描述

然后使用python3 manage.py createsuperuser命令创建超级用户:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM