When attempting to register a new user through the registration form, the phone number (phone_number) and library ID (library_id) fields are not being saved to the database. Despite these fields being included in the registration form and rendered correctly, the submitted values are not written the database.
views.py
def user_register(request):
userIsAuthenticated = False
if request.user.is_authenticated:
username1 = request.user.username
userIsAuthenticated = True
else:
username1 = "N/A"
userIsAuthenticated = False
if request.method == 'GET':
form = RegisterForm()
return render(request, "register.html", {"form":form, "username": username1, "userIsAuthenticated": userIsAuthenticated})
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
user.save()
login(request, user)
return render(request, "register-success.html")
else:
return render(request, 'register.html', {"form":form, "username": username1, "userIsAuthenticated": userIsAuthenticated})
forms.py
class RegisterForm(UserCreationForm):
first_name = forms.CharField(max_length=100)
last_name = forms.CharField(max_length=100)
library_id = forms.CharField(max_length=100)
phone_number = forms.CharField(max_length=100)
class Meta:
model = User
fields = ['first_name', 'last_name', 'username', 'email', 'password1', 'password2']
def save(self, commit=True):
user = super().save(commit=False)
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.email = self.cleaned_data['email']
if commit:
Profile.objects.create(
user=user,
phone_number=self.cleaned_data['phone_number'],
library_id=self.cleaned_data['library_id']
)
user.save()
return user
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone_number = models.CharField(max_length=100)
library_id = models.CharField(max_length=100)
def __str__(self):
return self.user.username
signals.py
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
More details (registration process, the problem in better detail):
- fill out the registration form
Register form on the website - let’s take a look at the db table
id | username | first_name | last_name | |
---|---|---|---|---|
27 | dolor | Lorem | ipsum | [email protected] |
The rest of the details are saved in the Profile model. Let’s look at it!
id | user_id | phone_number | library_id |
---|---|---|---|
12 | 27 | “” | “” |
Here, the data from the form we filled out are not saved.
Additionaly, I am able to fill out those fields (phone_number, library_id) using the admin panel, so the problem is most likely in the user_register function.
Václav is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.