2017-09-25 32 views
1
from django.contrib.auth.models import User 
from django.core.management.base import BaseCommand 


class Command(BaseCommand): 
    help = 'Create default system user without login authorization' 

    def handle(self, *args, **options): 
     User.objects.create_superuser('codobot', '[email protected]', None) 

我创建具有超级用户密码None
但是,当我看着数据库仍然在现场password数据。 开始!
创建一个Django的超级用户,但从来没有让它登录

问:
的是,用户可以登录?

+2

如果您想创建用户并且不让他登录,您可以使用* is_active *将其设置为False,并且用户无法登录到您的系统(如果您使用的是Django身份验证模型)。 – Thaian

+0

首先**没有**也可以有一个散列值,但如果django检测到None为raw_password,那么它会被设置为不可用的密码,这就是为什么数据在密码字段中的答案。 **!**只是由我猜测的密钥哈希组合引起的。 – zypro

+0

@Thaian。谢谢您的回复。只是意识到你的答案也是我的选择 – Sarit

回答

2

没有用户无法登录,

你可以简单的验证它:

from django.contrib.auth import authenticate 

user = authenticate(username='codobot', password=None) 
print ('Could not login') if user is None else ('User is logged in') 

通过文档

当raw_password是无,密码将被设置为不可用密码,就像使用了set_unusable_password()一样。

细节here

+0

请稍等3分钟。谢谢。 – Sarit

+0

https://docs.djangoproject.com/en/1.11/topics/auth/default/#django.contrib.auth.authenticate 它与'request'一起使用你怎么知道它可以与我的情况一起使用? – Sarit

+0

啊。 **凭据。我的坏 – Sarit

相关问题