簡體   English   中英

為什么我無法使用有效憑據登錄 Django 用戶,但可以登錄我原來的超級用戶?

[英]Why can't I log into Django users with valid credentials, but can log into my original superuser?

我的 django 項目中有這個問題,用戶在創建帳戶后無法登錄。 用戶憑據是正確的,所以一切都應該正常工作,但我想我在構建我的客戶用戶 Model 時忘記正確連接某些東西。

查看我的管理頁面,我看到數據庫中的用戶。 嘗試調試錯誤時,我也無法登錄帳戶的超級用戶版本(更改超級用戶 = True)。 我添加了一些我在網上找到的建議,例如

SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False

SESSION_ENGINE = 'django.contrib.sessions.backends.db'

在我的設置中,但仍然沒有成功。 我只能登錄我創建的原始超級用戶。

下面的代碼:

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

網址.py

    path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
    path('api/', include('bucket_api.urls', namespace='bucket_api')),

正如您所看到的,我試圖登錄到默認的 REST 框架 url 和我的管理面板頁面,除了我原來的超級用戶之外,問題仍然存在。 我不認為這是一個遷移問題,因為遷移沒有任何更改。

這是 GET 錯誤 = "GET /api/ HTTP/1.1" 403 4997

感謝@meet,我已經使用他推薦的修復解決了這個問題。 我還必須添加到我的settings.py

rest_framework.authentication.SessionAuthentication

像這樣:

    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    )

做這兩個修復解決了我的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM