Advertisement
Guest User

Untitled

a guest
Nov 28th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. class RegistrationFormView(View):
  2. form_class = RegistrationForm
  3. template_name = 'registration/registration_form.html'
  4.  
  5. # display blank form
  6. def get(self, request):
  7. form = self.form_class(None)
  8. return render(request, self.template_name, {'form': form})
  9.  
  10. # process form data
  11. def post(self, request):
  12. form = self.form_class(request.POST)
  13.  
  14. if form.is_valid():
  15.  
  16. user = form.save(commit=False)
  17.  
  18. # cleaned (normalized) data
  19. username = form.cleaned_data['username']
  20. password = form.cleaned_data['password']
  21. user.set_password(password)
  22. user.save()
  23.  
  24. # returns User objects if credentials are correcot
  25. user = authenticate(username=username, password=password)
  26.  
  27. if user is not None:
  28.  
  29. if user.is_active:
  30. login(request, user)
  31. return redirect('/dashboard/')
  32.  
  33. return render_to_response('registration/registration_form.html', c, context_instance=RequestContext(request))
  34.  
  35. class RegistrationForm(forms.ModelForm):
  36. email = forms.EmailField(max_length=30, widget=forms.TextInput(attrs={'placeholder': 'Email Address', 'required':True}))
  37. username = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'placeholder': 'Username','required':True}))
  38. password = forms.CharField(max_length=30, widget=forms.PasswordInput(attrs={'placeholder': 'Password','required':True}))
  39. password2 = forms.CharField(max_length=30, widget=forms.PasswordInput(attrs={'placeholder': 'Re-Enter Password','required':True}))
  40.  
  41. class Meta:
  42. """The model that is extended from django models and the fields below are specified to prevent abstraction"""
  43. model = User
  44. fields = ('email', 'username', 'password', 'password2')
  45.  
  46. def clean(self):
  47. cleaned_data = super(registrationForm, self).clean()
  48. email = cleaned_data.get('email')
  49. username = cleaned_data.get('username')
  50. password = cleaned_data.get('password')
  51. password2 = cleaned_data.get('password2')
  52.  
  53. #check if username exist
  54. user = User.objects.filter(username=username)
  55. if user:
  56. raise forms.ValidationError("this username is already exsist")
  57.  
  58. #check for password and re-enter password
  59. if password != password2:
  60. raise forms.ValidationError("Password does not match")
  61.  
  62. #check for email is system
  63. emails = User.objects.filter(email=email)
  64. if email:
  65. raise forms.ValidationError("this email is already registered")
  66.  
  67. return cleaned_data
  68.  
  69. <form class="text-left" method="post" action=".">
  70. {% csrf_token %}
  71. {{ form }}
  72. <input type="submit" value="{% trans 'Register' %}" />
  73. </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement