Django how to create user with advanced model?

Good evening! The question itself in the header.
Made a model for Hyde: dunmaksim.blogspot.ru/2015/05/django-18.html

And here is the question. I'm just learning django more.
Searched through the entire Internet.
I was able to make entrance through registration redux
and now.

Please show the code desirable, as I register users?
I.e. with the usual registration form. And yet, do not want the activation email on the email flew. It is possible to remove?

Thank you very much
July 12th 19 at 17:11
2 answers
July 12th 19 at 17:13
Solution
class UserRegistrationForm(forms.ModelForm):
 password = forms.CharField(label='Password', widget=forms.PasswordInput)
 password2 = forms.CharField(label='Repeat password', widget=forms.PasswordInput)

 class Meta:
 model = User
 fields = ('username', 'first_name', 'email')

 def clean_password2(self):
 cd = self.cleaned_data
 if cd['password'] != cd['password2']:
 raise forms.ValidationError('Passwords don\'t match.')
 return cd['password2']

view
def register(request):
 if request.method == 'POST':
 user_form = UserRegistrationForm(request.POST)

 if user_form.is_valid():
 # Create a new user object but avoid saving it yet
 new_user = user_form.save(commit=False)
 # Set the chosen password
new_user.set_password(user_form.cleaned_data['password'])
 # Save the User object
new_user.save()
 return render(request,
'template_done.html',
 {'new_user': new_user})
else:
 user_form = UserRegistrationForm()
 return render(request, 'template.html', {'user_form': user_form})
Thank you! Not working, but here I will deal with the problems)) - Dixie.Schuster commented on July 12th 19 at 17:16
Have already figured out. Everything works as it should! Thank you very much! Understand how to use them - Dixie.Schuster commented on July 12th 19 at 17:19
July 12th 19 at 17:15
Here is my code, if anyone is interested
forms.py
class UserCreationForm(forms.ModelForm):
 password1 = forms.CharField(
label='passwd',
widget=forms.PasswordInput(attrs={
 'style':'margin:10px; padding:10px;height:40px',
 'class':'form-control col-sm-8',
 'placeholder': 'Password please'
})
)
 password2 = forms.CharField(
 label='double passwd',
widget=forms.PasswordInput(attrs={
 'style':'margin:10px; padding:10px;height:40px',
 'class':'form-control col-sm-8',
 'placeholder': 'Double' password please'
})
)

 def clean_password2(self):
 password1 = self.cleaned_data.get('password1')
 password2 = self.cleaned_data.get('password2')
 if password1 and password2 and password1 != password2:
 raise forms.ValidationError('Passwd passwd and double error')
 return password2


 def save(self, commit=True):
 user = super(UserCreationForm, self).save(commit=False)
 user.set_password(self.cleaned_data['password1']) # the important thing here is to pass password1 or 2. But no password as in the example above
 if commit:
user.save()
 return user


 class Meta:
 model = get_user_model()
 fields = ('email', 'lastname', 'firstname')
 widgets = {
 'email': forms.TextInput(attrs={
 'style':'margin:10px; padding:10px;height:40px',
 'class':'form-control col-sm-8',
 'placeholder': 'E-mail please'
}),
 'lastname': forms.TextInput(attrs={
 'style':'margin:10px; padding:10px;height:40px',
 'class':'form-control col-sm-8',
 'placeholder': 'Lastname please'
}),
 'firstname': forms.TextInput(attrs={
 'style':'margin:10px; padding:10px;height:40px',
 'class':'form-control col-sm-8',
 'placeholder': 'Firstname please'
}),
 }


Here is the view:
def registerPost(request):
 if request.method == "POST":
 user_form = UserCreationForm(request.POST)

 if user_form.is_valid():
 new_user = user_form.save(commit=False)
new_user.set_password(user_form.cleaned_data['password1'])
 new_user.avatar = "fotos/no_image_dummy.png"
new_user.save()
 return HttpResponseRedirect("/login")
else:
 user_form = UserCreationForm()
 return render(request, 'register.html', {'form':user_form})

Find more questions by tags OOPDjangoPython