1
我很感谢自定义用户模块的帮助。我在django documentation中将这个例子设置为模拟。我有以下问题:登录到Django的管理员
- 登录管理界面不起作用。登录页面显示出来,但在使用shell中的./manage createsuperuser创建用户后不接受我的凭据。
- 创建超级用户时,它会以明文形式保存初始密码。我看了一下数据库并以明文形式找到了密码。我猜这是来自create_superuser(),其中没有使用user.set_password(),但password = password(正如django文档中的例子,为什么他们会这么做?)。我在shell中更改它,然后加密。登录仍然没有工作寿。
我的代码如下: 认证/ models.py
class UserManager(BaseUserManager):
def create_user(self, email, password=None, **kwargs):
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=self.normalize_email(email),
**kwargs
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password, **kwargs):
user = self.model(
email,
password=password,
**kwargs
)
user.is_admin=True
user.save(using=self._db)
return user
和
class MyUser(AbstractBaseUser):
# use this for auth and sessions
# REQUIRED_FIELDS = ['password']
session = models.ForeignKey(
'sessions.Session',
verbose_name='Session',
blank=True, null=True,
)
email = models.EmailField(unique=True, primary_key=True)
#password = forms.CharField(max_length=30, widget=forms.PasswordInput()) #render_value=False
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
signed_up_since = models.DateTimeField('Signed up since', default=timezone.now())
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'email'
def __str__(self):
return self.email
def get_full_name(self):
return self.email
def get_short_name(self):
return self.email
@property
def is_staff(self):
"Is the user a member of staff?"
# Simplest possible answer: All admins are staff
return self.is_admin
我编辑的设置:
AUTH_USER_MODEL = "authentication.MyUser"
我不使用自定义会话后端或身份验证后端,迁移sql,版本等壳牌给我这个:
>>> user.is_staff
True
任何想法?非常感谢!