Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. class UserProfile(models.Model):
  2. user = models.ForeignKey(User, unique=True, related_name="connect")
  3. location = models.CharField(max_length=20, blank=True, null=True)
  4.  
  5. #UserCreationForm for User Model
  6.  
  7. class UserProfileForm(ModelForm):
  8. class Meta:
  9. model = UserProfile
  10. exclude = ("user", )
  11.  
  12. def register(request):
  13. if request.method == 'POST':
  14. form1 = UserCreationForm(request.POST)
  15. form2 = UserProfileForm(request.POST)
  16. if form1.is_valid():
  17. #create initial entry for User object
  18. username = form1.cleaned_data["username"]
  19. password = form1.cleaned_data["password"]
  20. new_user = User.objects.create_user(username, password)
  21.  
  22. # What to do here to save "location" field in a UserProfile
  23. # object that corresponds with the new_user User object that
  24. # we just created in the previous lines
  25.  
  26. else:
  27. form1 = UserCreationForm()
  28. form2 = UserProfileForm()
  29. c = {
  30. 'form1':UserCreationForm,
  31. 'form2':form2,
  32. }
  33. c.update(csrf(request))
  34. return render_to_response("registration/register.html", c)
  35.  
  36. def register(request):
  37. if request.method == 'POST':
  38. form1 = UserCreationForm(request.POST)
  39. form2 = UserProfileForm(request.POST)
  40. if form1.is_valid() and form2.is_valid():
  41. #create initial entry for User object
  42. user = form1.save()
  43. userprofile = form2.save(commit=False)
  44. userprofile.user = user
  45. userprofile.location = get_the_location_somehow()
  46. userprofile.save()
  47.  
  48. # What to do here to save "location" field in a UserProfile
  49. # object that corresponds with the new_user User object that
  50. # we just created in the previous lines
  51.  
  52. else:
  53. form1 = UserCreationForm()
  54. form2 = UserProfileForm()
  55. c = {
  56. 'form1':UserCreationForm,
  57. 'form2':form2,
  58. }
  59. c.update(csrf(request))
  60. return render_to_response("registration/register.html", c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement