如何重置Django管理密码?

时间:2022-11-15 10:02:37

I am using Django (version 1.3) and have forgotten both admin username and password. How to reset both?

我正在使用Django(版本1.3),并且忘记了管理用户名和密码。如何重置吗?

And is it possible to make a normal user into admin, and then remove admin status?

是否可以将一个普通的用户变成admin,然后删除admin ?

14 个解决方案

#1


511  

manage.py changepassword <user_name>

see docs

看文档

#2


139  

  1. python manage.py createsuperuser will create another superuser, you will be able to log into admin and rememder your username.
  2. python管理。py createsuperuser将创建另一个超级用户,您可以登录到admin并记住您的用户名。
  3. Yes, why not.
  4. 是的,为什么不。

To give a normal user privileges, open a shell with python manage.py shell and try:

要授予普通的用户权限,请使用python管理打开一个shell。py壳牌和尝试:

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

#3


84  

You may try through console:

您可以通过控制台尝试:

python manage.py shell

then use following script in shell

然后在shell中使用以下脚本

from django.contrib.auth.models import User
User.objects.filter(is_superuser=True)

will list you all super users on the system. if you recognize yur username from the list:

将列出系统上所有的超级用户。如果您从列表中识别您的用户名:

usr = User.objects.get(username='your username')
usr.set_password('raw password')
usr.save()

and you set a new password (:

设置一个新密码(:

#4


19  

You can create a new superuser with createsuperuser command.

您可以使用createsuperuser命令创建一个新的超级用户。

#5


9  

This is very good question.

这是个很好的问题。

python manage.py changepassword user_name

python管理。py changepassword user_name

Example :-

例子:-

python manage.py changepassword mickey

#6


9  

new setup should first run python manage.py createsuperuser to create user. It seems like there is no default username password to login into admin.

新的设置应该首先运行python管理。py创建一个用户。似乎没有默认的用户名密码登录管理。

#7


4  

You may also have answered a setup question wrong and have zero staff members. In which case head to postgres:

您可能也回答了设置问题,并且没有工作人员。在这种情况下,向postgres:

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

To fix, edit directly:

修复,编辑直接:

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

#8


3  

If you forgot create admin user first build one with createsuperuser command on manage.py then change the password.

如果您忘记创建管理用户,请首先使用createsuperusercommand在manage中构建一个。然后修改密码。

#9


3  

One of the best ways to retrieve the username and password is to view and update them. The User Model provides a perfect way to do so.

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

In this case, I'm using Django 1.9

  1. Navigate to your root directory i,e. where you "manage.py" file is located using your console or other application such as Git.
  2. 导航到根目录i e。你在哪里”管理。py文件使用控制台或其他应用程序(如Git)定位。
  3. Retrieve the Python shell using the command "python manage.py shell".
  4. 使用“Python管理”命令检索Python shell。py壳”。
  5. Import the User Model by typing the following command "from django.contrib.auth.models import User"
  6. 通过输入以下命令“从django.致歉.auth”导入用户模型。模型导入用户”
  7. Get all the users by typing the following command "users = User.objects.all()"
  8. 通过输入以下命令“users = User.objects.all()”获取所有用户
  9. 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.

    为Python 2用户打印一份用户列表,使用“打印用户”命令为Python 3用户使用“Print(用户)”命令。第一个用户通常是管理员。

  10. Select the user you wish to change their password e.g.

    选择您希望更改其密码的用户。

    "user = users[0]"

    “用户[0]=用户”

  11. Set the password

    设置密码

    user.set_password('name_of_the_new_password_for_user_selected')

    user.set_password(“name_of_the_new_password_for_user_selected”)

  12. Save the new password

    保存新密码

    "user.save()"

    “user.save()”

Start the server and log in using the username and the updated password.

启动服务器并使用用户名和更新的密码登录。

#10


2  

In case you do not know the usernames as created here. You can get the users as described by @FallenAngel above.

如果您不知道这里创建的用户名。您可以获得上面@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

However in the event that you created your independent user model. A simple case is when you want to use email as a username instead of the default user name. In which case your user model lives somewhere such as your_accounts_app.models then the above solution wont work. In this case you can instead use the get_user_model method

但是,如果您创建了独立的用户模型。一个简单的例子是,当您想使用电子邮件作为用户名而不是默认用户名时。在这种情况下,用户模型位于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

#11


2  

You may try this:

你可以试试这个:

1.Change Superuser password without console

1。更改超级用户密码而不使用控制台。

python manage.py changepassword <username>

2.Change Superuser password through console

2。通过控制台更改超级用户密码。

如何重置Django管理密码? 如何重置Django管理密码?

#12


1  

Another thing that is worth noting is to set your user's status is_staff as active. At least, that's what makes it works for me. For more detail, I created another superuser as people explained above. Then I go to the database table auth_user and search for that username to make sure its is_staff flag is set to 1. That finally allowed me to log into admin site.

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

#13


0  

python manage.py changepassword username

python管理。py changepassword用户名

#14


0  

Create a new superuser with the command "python manage.py createsuperuser". Login as the new super user. Click on the 'users' link. Then click on the user you want to delete. click on delete user at the end of the form page.

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

Note - The above process will make changes to the activity logs done by that particular user.

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

#1


511  

manage.py changepassword <user_name>

see docs

看文档

#2


139  

  1. python manage.py createsuperuser will create another superuser, you will be able to log into admin and rememder your username.
  2. python管理。py createsuperuser将创建另一个超级用户,您可以登录到admin并记住您的用户名。
  3. Yes, why not.
  4. 是的,为什么不。

To give a normal user privileges, open a shell with python manage.py shell and try:

要授予普通的用户权限,请使用python管理打开一个shell。py壳牌和尝试:

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

#3


84  

You may try through console:

您可以通过控制台尝试:

python manage.py shell

then use following script in shell

然后在shell中使用以下脚本

from django.contrib.auth.models import User
User.objects.filter(is_superuser=True)

will list you all super users on the system. if you recognize yur username from the list:

将列出系统上所有的超级用户。如果您从列表中识别您的用户名:

usr = User.objects.get(username='your username')
usr.set_password('raw password')
usr.save()

and you set a new password (:

设置一个新密码(:

#4


19  

You can create a new superuser with createsuperuser command.

您可以使用createsuperuser命令创建一个新的超级用户。

#5


9  

This is very good question.

这是个很好的问题。

python manage.py changepassword user_name

python管理。py changepassword user_name

Example :-

例子:-

python manage.py changepassword mickey

#6


9  

new setup should first run python manage.py createsuperuser to create user. It seems like there is no default username password to login into admin.

新的设置应该首先运行python管理。py创建一个用户。似乎没有默认的用户名密码登录管理。

#7


4  

You may also have answered a setup question wrong and have zero staff members. In which case head to postgres:

您可能也回答了设置问题,并且没有工作人员。在这种情况下,向postgres:

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

To fix, edit directly:

修复,编辑直接:

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

#8


3  

If you forgot create admin user first build one with createsuperuser command on manage.py then change the password.

如果您忘记创建管理用户,请首先使用createsuperusercommand在manage中构建一个。然后修改密码。

#9


3  

One of the best ways to retrieve the username and password is to view and update them. The User Model provides a perfect way to do so.

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

In this case, I'm using Django 1.9

  1. Navigate to your root directory i,e. where you "manage.py" file is located using your console or other application such as Git.
  2. 导航到根目录i e。你在哪里”管理。py文件使用控制台或其他应用程序(如Git)定位。
  3. Retrieve the Python shell using the command "python manage.py shell".
  4. 使用“Python管理”命令检索Python shell。py壳”。
  5. Import the User Model by typing the following command "from django.contrib.auth.models import User"
  6. 通过输入以下命令“从django.致歉.auth”导入用户模型。模型导入用户”
  7. Get all the users by typing the following command "users = User.objects.all()"
  8. 通过输入以下命令“users = User.objects.all()”获取所有用户
  9. 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.

    为Python 2用户打印一份用户列表,使用“打印用户”命令为Python 3用户使用“Print(用户)”命令。第一个用户通常是管理员。

  10. Select the user you wish to change their password e.g.

    选择您希望更改其密码的用户。

    "user = users[0]"

    “用户[0]=用户”

  11. Set the password

    设置密码

    user.set_password('name_of_the_new_password_for_user_selected')

    user.set_password(“name_of_the_new_password_for_user_selected”)

  12. Save the new password

    保存新密码

    "user.save()"

    “user.save()”

Start the server and log in using the username and the updated password.

启动服务器并使用用户名和更新的密码登录。

#10


2  

In case you do not know the usernames as created here. You can get the users as described by @FallenAngel above.

如果您不知道这里创建的用户名。您可以获得上面@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

However in the event that you created your independent user model. A simple case is when you want to use email as a username instead of the default user name. In which case your user model lives somewhere such as your_accounts_app.models then the above solution wont work. In this case you can instead use the get_user_model method

但是,如果您创建了独立的用户模型。一个简单的例子是,当您想使用电子邮件作为用户名而不是默认用户名时。在这种情况下,用户模型位于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

#11


2  

You may try this:

你可以试试这个:

1.Change Superuser password without console

1。更改超级用户密码而不使用控制台。

python manage.py changepassword <username>

2.Change Superuser password through console

2。通过控制台更改超级用户密码。

如何重置Django管理密码? 如何重置Django管理密码?

#12


1  

Another thing that is worth noting is to set your user's status is_staff as active. At least, that's what makes it works for me. For more detail, I created another superuser as people explained above. Then I go to the database table auth_user and search for that username to make sure its is_staff flag is set to 1. That finally allowed me to log into admin site.

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

#13


0  

python manage.py changepassword username

python管理。py changepassword用户名

#14


0  

Create a new superuser with the command "python manage.py createsuperuser". Login as the new super user. Click on the 'users' link. Then click on the user you want to delete. click on delete user at the end of the form page.

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

Note - The above process will make changes to the activity logs done by that particular user.

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