Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs “如何修复”;“400错误请求”;rest身份验证/注册/?_Reactjs_Django Rest Framework_React Redux - Fatal编程技术网

Reactjs “如何修复”;“400错误请求”;rest身份验证/注册/?

Reactjs “如何修复”;“400错误请求”;rest身份验证/注册/?,reactjs,django-rest-framework,react-redux,Reactjs,Django Rest Framework,React Redux,我使用react、react-redux进行注册,而django rest api创建用户注册序列化程序。我是个新手。我每次尝试注册时都会收到404错误请求 首先在django中,我创建了custoom用户 class UserManager(BaseUserManager): use_in_migrations = True def create_user(self, username, email, first_name, last_name, role_id, conta

我使用react、react-redux进行注册,而django rest api创建用户注册序列化程序。我是个新手。我每次尝试注册时都会收到404错误请求

首先在django中,我创建了custoom用户

class UserManager(BaseUserManager):
    use_in_migrations = True

    def create_user(self, username, email, first_name, last_name, role_id, contact_num, opt_contact_num, citizenship_num, password=None):
        user = self.model(
            username=username,
            email=self.normalize_email(email),
            first_name=first_name,
            last_name=last_name,
            role_id=role_id,
            contact_num=contact_num,
            opt_contact_num=opt_contact_num,
            citizenship_num=citizenship_num,
        )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_staffuser(self,username, email, first_name, last_name, role_id, contact_num, opt_contact_num, citizenship_num, password):
        user = self.create_user(
            username=username,
            email=email,
            first_name=first_name,
            last_name=last_name,
            role_id=role_id,
            contact_num=contact_num,
            opt_contact_num=opt_contact_num,
            citizenship_num=citizenship_num,
        )
        user.is_staff = True
        user.save(using=self._db)
        return user

    def create_superuser(self, username, email, first_name, last_name, role_id, contact_num, opt_contact_num, citizenship_num, password):
        user = self.create_user(
            username=username,
            email=email,
            first_name="True",
            last_name="True",
            role_id=0,
            contact_num=contact_num,
            opt_contact_num=opt_contact_num,
            citizenship_num=citizenship_num,
            password=password,
        )
        user.is_staff = True
        user.is_admin = True
        user.save(using=self._db)
        return user

#custom user table
class User(AbstractBaseUser):
    username = models.CharField(max_length=120, unique=True)
    email = models.EmailField(unique=True)
    first_name = models.CharField(max_length=120)
    last_name = models.CharField(max_length=120)
    role_id = models.PositiveIntegerField(default=0)
    contact_num = PhoneNumberField(null=False, blank=False)
    opt_contact_num = PhoneNumberField(null=True, blank=True)
    citizenship_num = models.CharField(max_length=120)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = [ 'email','first_name','last_name', 'role_id', 'contact_num', 'opt_contact_num', 'citizenship_num' ]
    is_admin = models.BooleanField(default=False)
    objects = UserManager()

    def is_superuser(self):
        return self.is_admin

    def is_staff(self):
       return self.is_admin

    def has_perm(self, perm, obj=None):
       return self.is_admin

    def has_module_perms(self, app_label):
       return self.is_admin

    def __str__(self):
        return self.username
然后包括自定义寄存器和详细信息序列化程序

class CustomRegisterSerializer(RegisterSerializer):
    username = serializers.CharField(required=True)
    email = serializers.EmailField(required=True)
    first_name = serializers.CharField(required=True)
    last_name = serializers.CharField(required=True)
    role_id = serializers.IntegerField(required=True)
    contact_num = serializers.IntegerField(required=True)
    opt_contact_num = serializers.IntegerField(allow_null=True)
    citizenship_num = serializers.CharField(required=True)
    password1 = serializers.CharField(write_only=True)
    password2 = serializers.CharField(write_only=True)

    def get_cleaned_data(self):
        super(CustomRegisterSerializer, self).get_cleaned_data()

        return {
            'username': self.validated_data.get('username', ''),
            'email': self.validated_data.get('email', ''),
            'first_name': self.validated_data.get('email', ''),
            'last_name': self.validated_data.get('last_name', ''),
            'role_id': self.validated_data.get('role_id', ''),
            'contact_num': self.validated_data.get('contact_num', ''),
            'opt_contact_num': self.validated_data.get('opt_contact_num', ''),
            'citizenship_num': self.validated_data.get('citizenship_num', ''),
            'password1': self.validated_data.get('password1', ''),
            'password2': self.validated_data.get('password2', ''),
        }

class CustomUserDetailsSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username','email','first_name', 'last_name', 'role_id', 'contact_num', 'opt_contact_num', 'citizenship_num')
        read_only_fields = ('username','email','first_name', 'last_name', 'role_id', 'contact_num', 'opt_contact_num', 'citizenship_num')
以这种方式更改了settings.py

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.AllowAny'
    ]
}

ACCOUNT_EMAIL_VERIFICATION = 'none'

ACCOUNT_AUTHENTICATION_METHOD = 'username'

ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_UNIQUE_EMAIL = True
ACCOUNT_USERNAME_REQUIRED = True
ACCOUNT_UNIQUE_USERNAME = True
ACCOUNT_USER_EMAIL_FIELD = 'email'
ACCOUNT_LOGOUT_ON_GET = True

AUTH_USER_MODEL = 'menu.User'

REST_AUTH_SERIALIZERS = {
    "USER_DETAILS_SERIALIZER": "menu.api.serializers.CustomUserDetailsSerializer",
}

REST_AUTH_REGISTER_SERIALIZERS = {
    "REGISTER_SERIALIZER": "menu.api.serializers.CustomRegisterSerializer",
}

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = False

DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'

PHONENUMBER_DEFAULT_REGION = "NP"
我正在使用react和react redux注册用户:

export const authSignup = (username, email, first_name, last_name, role_id, contact_num, opt_contact_num, citizenship_num, password1, password2) => {
  return dispatch => {
    dispatch(authStart());
    axios.post('http://127.0.0.1:8000/rest-auth/registration/', {
      username: username,
      email: email,
      first_name: first_name,
      last_name: last_name,
      role_id: role_id,
      contact_num: contact_num,
      opt_contact_num: opt_contact_num,
      citizenship_num: citizenship_num,
      password1: password1,
      password2: password2,
    })
    .then(res => {
      const token = res.data.key;
      const expirationDate = new Date(new Date().getTime() + 3600 * 1000 * 24);
      localStorage.setItem('token', token);
      localStorage.setItem('username', username);
      localStorage.setItem('expirationDate', expirationDate);
      dispatch(getUserDetails(token, username));
    })
    .catch(err => {
      dispatch(authFail(err))
    })
  }
}
这是注册表单发送数据的内容:

Object
citizenship_num: "ajfahfl556"
confirm: "12345678"
contact_num: "9849829046"
email: "user@email.com"
first_name: "user"
last_name: "RMS"
opt_contact_num: undefined
password: "12345678"
role_id: "3"
userName: "user"

您是否尝试从链接末尾删除斜杠
/
,并发送Axios请求,如:
Axios.post('http://127.0.0.1:8000/rest-授权/注册',{
❓(=是的,我刚刚尝试从链接末尾删除斜杠
/
,现在显示500个服务器错误。这是我的请求数据``公民身份号码:“9849829046”确认:“12345678asd”联系人\u号码:“9849829046”电子邮件:user4@gmail.com“名字:”user4“姓氏:”Rms“opt_contact”num:“密码:”12345678asd“角色id:“3”用户名:“user4”“有人找到这个答案了吗?”?