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']
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()
profile = Profile.objects.create(user=new_user)
return redirect('/account/login/')
else:
user_form = UserRegistrationForm()
return render(request, 'account/register.html', {'user_form': user_form})
class Profile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL)
position_lat = models.DecimalField(decimal_places=8, max_digits=10, verbose_name="Широта")
position_long = models.DecimalField(decimal_places=8, max_digits=10, verbose_name="Довгота")
def __str__(self):
return 'Profile for user {}'.format(self.user.username)