Django manage.py sync_db 创建超级用户时崩溃
我在用 manage.py
的 syncdb
命令为我的 Django 应用创建初始数据库时,程序在我输入邮箱地址后,快要创建超级用户的时候崩溃了。
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/getpass.py", line 114, in fallback_getpass
stacklevel=2)
getpass.GetPassWarning: Can not control echo on the terminal.
为了解决这个问题,我尝试了以下几种方法:
在
syncdb
过程中跳过创建超级用户,直接运行createsupercommand
。这样似乎解决了问题,因为超级用户被创建了,但我却无法用这个用户的 ID 和密码登录admin site
。删除并重新创建数据库。
仔细检查数据库中的用户资料:用户名、密码、是否是员工、是否激活,这些值都是正确的。
调试过程,直到我看到正确的用户名和密码被传入
authenticate
函数。检查数据库编码,确认设置为 UTF-8。
我使用的是 Django 1.6.5、Pycharm 和 MySQL。整个事情让我感到困惑,因为我其他项目的设置完全一样,运行得很好。
有没有人知道这可能是什么原因呢?
编辑:
显然在创建超级用户的过程中,Django somehow 将密码标记为不可用(这很难发现,因为哈希值存储在数据库中,人无法解读)。我通过在 Django 环境外创建一个密码哈希并将其粘贴到数据库中来绕过这个问题。至少现在我可以登录了。不过问题还没有完全解决……
2 个回答
给未来的读者:
这个问题在Django 2.06、Python 3.6.1、Windows 7 SP1,以及使用PyCharm 2018.1时也出现过。
部分错误追踪信息:
File "..\django\contrib\auth\management\commands\createsuperuser.py", line 145, in handle password = getpass.getpass()
File "C:\Program Files (x86)\Python36-32\lib\getpass.py", line 122, in fallback_getpass
getpass.GetPassWarning: Can not control echo on the terminal.
这个问题是根据@vlad-ardelean在本线程中的建议解决的:
(i) 打开一个Windows终端(在PyCharm里打开也可以)
(ii) 进入包含manage.py的Django项目目录
(iii) 输入命令manage.py createsuperuser
在Ubuntu上,getpass
库是这样工作的
# Bind the name getpass to the appropriate function
try:
import termios
# it's possible there is an incompatible termios from the
# McMillan Installer, make sure we have a UNIX-compatible termios
termios.tcgetattr, termios.tcsetattr
except (ImportError, AttributeError):
try:
import msvcrt
except ImportError:
try:
from EasyDialogs import AskPassword
except ImportError:
getpass = fallback_getpass
else:
getpass = AskPassword
else:
getpass = win_getpass
else:
getpass = unix_getpass
你的问题似乎是因为getpass
库无法导入它需要的三个库中的一个,这些库是用来以不显示输入的方式读取用户输入的。
我知道你在用Mac,我不确定这是否会有所不同,但如果getpass
和我的一样,你需要确保安装了terminos
、msvcrt
或EasyDialogs
。
这篇StackOverflow上的帖子告诉你如何在Mac上安装这些包。
你基本上可以运行以下命令:easy_install pip
,然后再运行pip install terminos
(如果这样还不行,可以尝试安装msvcrt
或EasyDialogs
)。
[编辑] msvcrt
库只是针对Windows的,别在意这个。