Reputation: 39
I am using react, react redux for registration while django rest api to create user register serializers. I am a newbee. I am getting 404 bad request error every time i am trying to sign up?
First in django I created custoom user
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
Then included Custom Register and details Serializer
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')
Changed settings.py this way,
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"
I am using react and react redux for signing up user:
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))
})
}
}
This is what signup form sends data:
Object
citizenship_num: "ajfahfl556"
confirm: "12345678"
contact_num: "9849829046"
email: "[email protected]"
first_name: "user"
last_name: "RMS"
opt_contact_num: undefined
password: "12345678"
role_id: "3"
userName: "user"
Upvotes: 2
Views: 4325
Reputation: 360
Still experiencing this same issue 2 years later!
Inspect your error that returns in the catch block:
.catch(e => {
console.log(e.response.data);
})
/**
{
email: ['This field may not be blank.']
password1: ['This field may not be blank.']
password2: ['This field may not be blank.']
}
*/
Oddly, if you are using fetch, the catch block won't be triggered.
await fetch('http://localhost:8000/dj-rest-auth/login/', {method: "POST"})
.then(res => res.json())
.then(res => {console.log(res)})
will log the same result to the console
Upvotes: 2