Guest User

Untitled

a guest
Feb 17th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. class forms
  2. from django import forms
  3. from django.contrib.auth.models import User
  4. from django.contrib.auth.forms import UserCreationForm
  5. from accounts.models import UserProfile
  6.  
  7.  
  8. class RegistrationForm (UserCreationForm):
  9. email = forms.EmailField(required = True)
  10.  
  11. class Meta:
  12. model = User
  13. fields = ('username',
  14. 'first_name',
  15. 'last_name',
  16. 'email',
  17. 'password1',
  18. 'password2'
  19. )
  20. def save (self, commit= True):
  21. user = super(RegistrationForm,self).save(commit=True)
  22. user.first_name = self.cleaned_data['first_name']
  23. user.last_name = self.cleaned_data['last_name']
  24. user.email = self.cleaned_data['email']
  25. # user.set_password(self.cleaned_data["password1"])
  26.  
  27.  
  28. if commit:
  29. user.save()
  30.  
  31. return user
  32.  
  33. class RegistrationFormFull (UserCreationForm):
  34.  
  35. class Meta:
  36. model = UserProfile
  37. fields = ('city',
  38. 'discription',
  39. 'phone'
  40. )
  41.  
  42.  
  43.  
  44.  
  45. //class views
  46. from django.shortcuts import render
  47. from django.shortcuts import redirect
  48. from welcome.form import RegistrationForm,RegistrationFormFull
  49. from django.contrib.auth.decorators import login_required
  50. from django.db import transaction
  51. from accounts.models import UserProfile
  52. from django.contrib.auth.models import User
  53.  
  54.  
  55. def register (request) :
  56. if request.method == 'POST':
  57. user_form = RegistrationForm(request.POST,instance=request.User)
  58. full_form =
  59. RegistrationFormFull(request.POST,instance=request.User.UserProfile)
  60. if user_form.is_valid() and full_form.is_valid():
  61. user_form.save()
  62. full_form.save()
  63. return redirect('/home')
  64.  
  65. else:
  66. user_form = RegistrationForm(instance=request.user)
  67. full_form = RegistrationFormFull(instance=request.UserProfile)
  68. args = {'user_form':user_form,
  69. 'full_form':full_form
  70. }
  71. return render(request,'welcome/reg_form.html',args)
Add Comment
Please, Sign In to add comment