Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class RegistrationFormView(View):
- form_class = RegistrationForm
- template_name = 'registration/registration_form.html'
- # display blank form
- def get(self, request):
- form = self.form_class(None)
- return render(request, self.template_name, {'form': form})
- # process form data
- def post(self, request):
- form = self.form_class(request.POST)
- if form.is_valid():
- user = form.save(commit=False)
- # cleaned (normalized) data
- username = form.cleaned_data['username']
- password = form.cleaned_data['password']
- user.set_password(password)
- user.save()
- # returns User objects if credentials are correcot
- user = authenticate(username=username, password=password)
- if user is not None:
- if user.is_active:
- login(request, user)
- return redirect('/dashboard/')
- return render_to_response('registration/registration_form.html', c, context_instance=RequestContext(request))
- class RegistrationForm(forms.ModelForm):
- email = forms.EmailField(max_length=30, widget=forms.TextInput(attrs={'placeholder': 'Email Address', 'required':True}))
- username = forms.CharField(max_length=30, widget=forms.TextInput(attrs={'placeholder': 'Username','required':True}))
- password = forms.CharField(max_length=30, widget=forms.PasswordInput(attrs={'placeholder': 'Password','required':True}))
- password2 = forms.CharField(max_length=30, widget=forms.PasswordInput(attrs={'placeholder': 'Re-Enter Password','required':True}))
- class Meta:
- """The model that is extended from django models and the fields below are specified to prevent abstraction"""
- model = User
- fields = ('email', 'username', 'password', 'password2')
- def clean(self):
- cleaned_data = super(registrationForm, self).clean()
- email = cleaned_data.get('email')
- username = cleaned_data.get('username')
- password = cleaned_data.get('password')
- password2 = cleaned_data.get('password2')
- #check if username exist
- user = User.objects.filter(username=username)
- if user:
- raise forms.ValidationError("this username is already exsist")
- #check for password and re-enter password
- if password != password2:
- raise forms.ValidationError("Password does not match")
- #check for email is system
- emails = User.objects.filter(email=email)
- if email:
- raise forms.ValidationError("this email is already registered")
- return cleaned_data
- <form class="text-left" method="post" action=".">
- {% csrf_token %}
- {{ form }}
- <input type="submit" value="{% trans 'Register' %}" />
- </form>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement