[英]Why can't I log into Django users with valid credentials, but can log into my original superuser?
I have this issue in my django project where users cannot login after creating an account.我的 django 项目中有这个问题,用户在创建帐户后无法登录。 The user credentials are correct so everything should be working, but I guess I forgot to properly connect something while building my Customer User Model.用户凭据是正确的,所以一切都应该正常工作,但我想我在构建我的客户用户 Model 时忘记正确连接某些东西。
Looking at my admin page, I see users in the database.查看我的管理页面,我看到数据库中的用户。 I also cannot log into a superuser version of the account (changing superuser = True) when trying to de-bug the error.尝试调试错误时,我也无法登录帐户的超级用户版本(更改超级用户 = True)。 I've added some recommendations I found online such as我添加了一些我在网上找到的建议,例如
SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
in my settings, but still no success.在我的设置中,但仍然没有成功。 I'm only able to log into my original super-user that I created.我只能登录我创建的原始超级用户。
Code below:下面的代码:
models.py
class CustomAccountManager(BaseUserManager):
def create_superuser(self, email, user_name, first_name, last_name, password, **other_fields):
other_fields.setdefault('is_staff', True)
other_fields.setdefault('is_superuser', True)
other_fields.setdefault('is_active', True)
if other_fields.get('is_staff') is not True:
raise ValueError(
'Superuser must be assigned to is_staff=True.')
if other_fields.get('is_superuser') is not True:
raise ValueError(
'Superuser must be assigned to is_superuser=True.')
return self.create_user(email, user_name, first_name, last_name, password, **other_fields)
def create_user(self, email, user_name, first_name, last_name, password, **other_fields):
if not email:
raise ValueError(_('You must provide an email address'))
if not user_name:
raise ValueError(_('You must provide a username'))
if not first_name:
raise ValueError(_('You must provide your first name'))
if not last_name:
raise ValueError(_('You must provide your last name'))
if not password:
raise ValueError(_('You must provide your last name'))
email = self.normalize_email(email)
user = self.model(email=email, user_name=user_name,
first_name=first_name, last_name=last_name, password=password, **other_fields)
user.set_password(password)
user.save()
return user
class NewUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
user_name = models.CharField(max_length=150, unique=True)
first_name = models.CharField(max_length=150, blank=True)
last_name = models.CharField(max_length=150, blank=True)
start_date = models.DateTimeField(default=timezone.now)
#subscribed = models.BooleanField(default=None)
is_active = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
objects = CustomAccountManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['user_name', 'first_name', 'last_name']
def __str__(self):
return self.user_name
urls.py网址.py
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
path('api/', include('bucket_api.urls', namespace='bucket_api')),
as you can see im trying to log into the default REST framework url AND my admin panel page, the issues persists with both except for my original superuser.正如您所看到的,我试图登录到默认的 REST 框架 url 和我的管理面板页面,除了我原来的超级用户之外,问题仍然存在。 I dont think this is a migration issue because there aren't any changes to migrate.我不认为这是一个迁移问题,因为迁移没有任何更改。
here's the GET error = "GET /api/ HTTP/1.1" 403 4997
这是 GET 错误 = "GET /api/ HTTP/1.1" 403 4997
Thanks to @meet, I've solved the issue using his recommended fix.感谢@meet,我已经使用他推荐的修复解决了这个问题。 I also had to add to my settings.py
我还必须添加到我的settings.py
rest_framework.authentication.SessionAuthentication
like so:像这样:
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.SessionAuthentication',
'rest_framework_simplejwt.authentication.JWTAuthentication',
)
Doing both of these fixes solved my error.做这两个修复解决了我的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.