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

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


当前回答

Python manage.py createsuperuser将创建另一个超级用户,您将能够登录到admin并记住您的用户名。 是的,为什么不呢?

要赋予普通用户权限,请使用python manage.py shell打开shell并尝试:

from django.contrib.auth.models import User
user = User.objects.get(username='normaluser')
user.is_superuser = True
user.save()

其他回答

只需在命令行中输入以下命令:

python manage.py changepassword yourusername

Python manage.py createsuperuser将创建另一个超级用户,您将能够登录到admin并记住您的用户名。 是的,为什么不呢?

要赋予普通用户权限,请使用python manage.py shell打开shell并尝试:

from django.contrib.auth.models import User
user = User.objects.get(username='normaluser')
user.is_superuser = True
user.save()

检索用户名和密码的最佳方法之一是查看和更新它们。用户模型提供了一种完美的实现方法。

在本例中,我使用的是Django 1.9

Navigate to your root directory i,e. where you "manage.py" file is located using your console or other application such as Git. Retrieve the Python shell using the command "python manage.py shell". Import the User Model by typing the following command "from django.contrib.auth.models import User" Get all the users by typing the following command "users = User.objects.all()" Print a list of the users For Python 2 users use the command "print users" For Python 3 users use the command "print(users)" The first user is usually the admin. Select the user you wish to change their password e.g. "user = users[0]" Set the password user.set_password('name_of_the_new_password_for_user_selected') Save the new password "user.save()"

启动服务器,使用用户名和修改后的密码登录。

如果在/admin/网站需要一个reset_password链接,这是去的方式: https://docs.djangoproject.com/en/3.2/ref/contrib/admin/#adding-a-password-reset-feature

使用"python manage.py createsuperuser"命令创建一个新的超级用户。 以新的超级用户登录。点击“users”链接。然后单击要删除的用户。单击表单页面末尾的delete user。

注意:上述过程将对特定用户所做的活动日志进行更改。