Guest User

Untitled

a guest
Oct 22nd, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. class Profile(models.Model):
  2. user = models.OneToOneField(User, on_delete=models.CASCADE)
  3. email = models.EmailField()
  4. img = models.FileField(upload_to='media/', blank=True, null=True)
  5.  
  6. def __str__(self):
  7. return self.user.username
  8. @receiver(post_save, sender=User)
  9. def update_user_profile(sender, instance, created, **kwargs):
  10. if created:
  11. Profile.objects.create(user=instance)
  12. instance.profile.save()
  13.  
  14. def signup(request):
  15. if request.method == 'POST':
  16. form = SignUpForm(request.POST)
  17. if form.is_valid():
  18. user = form.save()
  19. user.refresh_from_db() # load the profile instance created by the signal
  20. user.profile.email = form.cleaned_data.get('email')
  21. user.profile.img = form.cleaned_data.get('img')
  22. user.save()
  23. raw_password = form.cleaned_data.get('password1')
  24. user = authenticate(username=user.username, password=raw_password)
  25. login(request, user)
  26. return redirect('home')
  27. else:
  28. form = SignUpForm()
  29. return render(request, 'tforum/signup.html', {'form': form})
  30.  
  31. class SignUpForm(UserCreationForm):
  32. email = forms.EmailField(help_text='Required.')
  33. img = forms.FileField(help_text='Upload Image')
  34. class Meta:
  35. model = User
  36. fields = ('username', 'email', 'img', 'password1', 'password2', )
  37.  
  38. <form method="post" enctype="multipart/form-data">
  39. {% csrf_token %}
  40. {% for field in form %}
  41. <p>
  42. {{ field.label_tag }}<br>
  43. {{ field }}
  44. {% if field.help_text %}
  45. <small style="color: grey">{{ field.help_text }}</small>
  46. {% endif %}
  47. {% for error in field.errors %}
  48. <p style="color: red">{{ error }}</p>
  49. {% endfor %}
  50. </p>
  51. {% endfor %}
  52. <button type="submit">Sign up</button>
Add Comment
Please, Sign In to add comment