我正在使用Django(1.3版),忘记了管理员用户名和密码。如何重置两者?

是否可以将普通用户变为管理员,然后删除管理员状态?


当前回答

你也可能回答了一个错误的设置问题,没有工作人员。在这种情况下,前往postgres:

obvioustest=# \c [yourdatabasename]
obvioustest=# \x
obvioustest=# select * from auth_user;
-[ RECORD 1 ]+-------------
id           | 1
is_superuser | f
is_staff     | f
...

要修复,直接编辑:

update auth_user set is_staff='true' where id=1;

其他回答

你也可能回答了一个错误的设置问题,没有工作人员。在这种情况下,前往postgres:

obvioustest=# \c [yourdatabasename]
obvioustest=# \x
obvioustest=# select * from auth_user;
-[ RECORD 1 ]+-------------
id           | 1
is_superuser | f
is_staff     | f
...

要修复,直接编辑:

update auth_user set is_staff='true' where id=1;

可使用createsuperuser命令创建新的超级用户。

以防您不知道这里创建的用户名。您可以获得上述@FallenAngel所描述的用户。

python manage.py shell 
from django.contrib.auth.models import User
usrs = User.objects.filter(is_superuser=True)
#identify the user
your_user = usrs.filter(username="yourusername")[0]
#youruser = usrs.get(username="yourusername")
#then set the password

但是,如果您创建了独立的用户模型。一个简单的例子是当您想使用电子邮件作为用户名而不是默认用户名时。在这种情况下,您的用户模型位于your_accounts_app之类的地方。模型那么上面的解决方案将不起作用。 在这种情况下,您可以使用get_user_model方法

from django.contrib.auth import get_user_model 
super_users = get_user_model().objects.filter(is_superuser=True)
#proceed to get identify your user
# and set their user password

如果您忘记先创建admin用户,则在manage.py上使用createsuperuser命令创建一个admin用户,然后更改密码。

另一件值得注意的事情是将用户的状态is_staff设置为活动。至少,这对我来说管用。为了更详细地了解,我创建了另一个超级用户,如上所述。然后转到数据库表auth_user并搜索该用户名,以确保其is_staff标志设置为1。这终于允许我登录管理网站。