Guest User

Untitled

a guest
Dec 15th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. from django import forms
  2. from django.contrib.auth import get_user_model
  3.  
  4. class Register(forms.ModelForm):
  5. password = forms.CharField(widget=forms.PasswordInput)
  6. password2 = forms.CharField(label='Confirm password', widget=forms.PasswordInput)
  7.  
  8.  
  9.  
  10. class Meta:
  11. User = get_user_model()
  12. model = User
  13. fields = ('username', 'number' , 'country' , 'city' , 'email')
  14.  
  15. def clean_username(self):
  16. User = get_user_model()
  17. username = self.cleaned_data.get('username')
  18. qs = User.objects.filter(username=username)
  19. if qs.exists():
  20. raise forms.ValidationError("username is taken")
  21. return username
  22.  
  23. def clean_password2(self):
  24. # Check that the two password entries match
  25. password1 = self.cleaned_data.get("password")
  26. password2 = self.cleaned_data.get("password2")
  27. if password1 and password2 and password1 != password2:
  28. raise forms.ValidationError("Passwords don't match")
  29. return password2
  30.  
  31.  
  32. class UserAdminCreationForm(forms.ModelForm):
  33. """A form for creating new users. Includes all the required
  34. fields, plus a repeated password."""
  35. password = forms.CharField(label='Password', widget=forms.PasswordInput)
  36. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  37.  
  38. class Meta:
  39. User = get_user_model()
  40. model = User
  41. fields = ('username', 'number' , 'country' , 'city' , 'email')
  42.  
  43. def clean_password2(self):
  44. # Check that the two password entries match
  45. password1 = self.cleaned_data.get("password")
  46. password2 = self.cleaned_data.get("password2")
  47. if password1 and password2 and password1 != password2:
  48. raise forms.ValidationError("Passwords don't match")
  49. return password2
  50.  
  51. def save(self, commit=True):
  52. # Save the provided password in hashed format
  53. user = super(UserAdminCreationForm , self).save(commit=False)
  54. user.set_password(self.cleaned_data["password"])
  55. if commit:
  56. user.save()
  57. return user
  58.  
  59. @csrf_exempt
  60. def Home(request):
  61. if request.method == 'POST':
  62. form = Register(request.POST)
  63. if form.is_valid():
  64. form.save()
  65. return HttpResponse("greate")
  66. else:
  67. form = Register()
  68. return render_to_response('home.html' , {'form' : form})
Add Comment
Please, Sign In to add comment