我正在使用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 and 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"