I am using normal User model in Django to save my users. I would like to add first_name and last_name field, but I don't know how to extend my model and make it work. First and last name should be added in in register form (I use crispy forms).
register view
if request.method == "POST":
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save() # save user to database
username = form.cleaned_data.get('username')
messages.success(request, f"User {username} succesfully created! You can login now!")
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'users/register.html', {'form': form})
register form
def __init__(self, *args, **kwargs):
super(UserRegisterForm, self).__init__(*args, **kwargs)
self.fields['password1'].label = "Hasło"
self.fields['password2'].label = "Powtórz hasło"
email = forms.EmailField()
name = forms.CharField(
label="Imię",
required=True,
max_length=30,
)
surname = forms.CharField(
label="Nazwisko",
required=True,
max_length=30,
)
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2', 'name', 'surname']
labels = {
'username': 'Nazwa użytkownika',
}
Usermodel already hasfirst_nameandlast_namee.