Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django import forms
- from django.contrib.auth import get_user_model
- class Register(forms.ModelForm):
- password = forms.CharField(widget=forms.PasswordInput)
- password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)
- class Meta:
- User = get_user_model()
- model = User
- fields = ('username', 'number' , 'country' , 'city' , 'email')
- def clean_username(self):
- User = get_user_model()
- username = self.cleaned_data.get('username')
- qs = User.objects.filter(username=username)
- if qs.exists():
- raise forms.ValidationError("username is taken")
- return username
- def clean_password2(self):
- # Check that the two password entries match
- password1 = self.cleaned_data.get("password")
- password2 = self.cleaned_data.get("password2")
- if password1 and password2 and password1 != password2:
- raise forms.ValidationError("Passwords don't match")
- return password2
- class UserAdminCreationForm(forms.ModelForm):
- """A form for creating new users. Includes all the required
- fields, plus a repeated password."""
- password = forms.CharField(label='Password', widget=forms.PasswordInput)
- password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
- class Meta:
- User = get_user_model()
- model = User
- fields = ('username', 'number' , 'country' , 'city' , 'email')
- def clean_password2(self):
- # Check that the two password entries match
- password1 = self.cleaned_data.get("password")
- password2 = self.cleaned_data.get("password2")
- if password1 and password2 and password1 != password2:
- raise forms.ValidationError("Passwords don't match")
- return password2
- def save(self, commit=True):
- # Save the provided password in hashed format
- user = super(UserAdminCreationForm , self).save(commit=False)
- user.set_password(self.cleaned_data["password"])
- if commit:
- user.save()
- return user
- @csrf_exempt
- def Home(request):
- if request.method == 'POST':
- form = Register(request.POST)
- if form.is_valid():
- form.save()
- return HttpResponse("greate")
- else:
- form = Register()
- return render_to_response('home.html' , {'form' : form})
Add Comment
Please, Sign In to add comment