简体   繁体   English

为什么我无法使用有效凭据登录 Django 用户,但可以登录我原来的超级用户?

[英]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.

相关问题 无法登录在django admin后端创建的超级用户 - Can't log in super users created on the django admin backend 用户创建时没有密码 Django(用户无法登录) - user created with no password Django (users can't log in) Django 无法使用有效凭据登录 - Django can't login with valid credentials 为什么我的Django应用程序不能写入其日志文件? - Why can my Django app not write to its log file? 使用Django rest-auth进行的身份验证仅适用于超级用户。 在其他情况下,我得到-无法使用提供的凭据登录 - Authentication with Django rest-auth works properly only with a superuser. In other cases I get - Unable to log in with provided credentials 为什么我不能从我的 Flask 应用程序控制台日志数据? 我正在使用 jinja - Why can't I console log data from my flask application? I am using jinja 无法使用我创建的用户登录django的管理页面 - Can't log in admin page of the django using the user i created Django 无法在管理页面中登录超级用户 - Django can't login the superuser in admin page PYODBC要求提供凭据,尽管这是我的cxn代码的一部分,如何使它自动登录 - PYODBC asks for credentials although It is part of my cxn code, how can I have it automatically log in Django:无法使用普通身份验证登录 - Django: Can't log in using normal auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM