Guest User

Untitled

a guest
Nov 21st, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. class UserForm(forms.ModelsForm):
  2. email = form.CharField(widget=forms.EmailInput)
  3. password = form.CharField(widget=forms.PasswordInput)
  4.  
  5. class Meta:
  6. model = *write your model name here*
  7. fields = *fields from your model. The model design should include
  8. username email password and other attributes*
  9.  
  10. from django.views.generic import View
  11. from django.shortcuts import render, redirect
  12.  
  13. class UserFormView(View):
  14. form_class = UserForm
  15. template_name = *your html template*
  16.  
  17. def get(self, request):
  18. form = self.form_class(None)
  19. return render(request, self.template_name, {'form':form})
  20.  
  21. def post(self, request):
  22. form = self.form_class(request.POST)
  23.  
  24. if form.is_valid():
  25. user = form.save(commit=False)
  26. username = form.cleaned_data['username']
  27. password = form.cleaned_data['password']
  28. user.set_password(password)
  29. user.save()
  30.  
  31. user = authenticate(username=username, password=password)
  32. if user is not None and user.is_active:
  33. login(request, user)
  34. return redirect(*your redirecting url*)
  35. return render(request, self.template_name, {'form':form})
Add Comment
Please, Sign In to add comment