Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def create_user(request) -> HttpResponse:
- if request.method != 'POST':
- return render(request, 'Bartr/HTML/SignUp.html')
- dict= json.loads(request.body)
- if "username" not in dict or "password" not in dict:
- return HttpResponse(status=400)
- username = dict["username"]
- user_exist = {}
- user_exist["value"] = exists(username)
- # if user exists return false and terminate
- if user_exist["value"] == True:
- return HttpResponse(
- json.dumps(user_exist),
- content_type="application/json"
- )
- ## continue if user doesnt exist
- else:
- password = dict["password"]
- response_data = {}
- form = UserForm(dict)
- user = form.save(commit = False)
- user.set_password(password)
- user.first_name = dict["name"]
- user.save()
- if user is not None:
- if user.is_active:
- login(request,user)
- return HttpResponse(
- json.dumps(response_data),
- content_type="application/json"
- )
- class Profile(models.Model):
- user = models.OneToOneField(User, on_delete=models.CASCADE)
- email = models.TextField(max_length=100, blank=True)
- contact_number = models.CharField(max_length=20, blank=True)
- @receiver(post_save, sender=User)
- def create_user_profile(sender, instance, created, **kwargs):
- if created:
- Profile.objects.create(user=instance)
- @receiver(post_save, sender=User)
- def save_user_profile(sender, instance, **kwargs):
- instance.profile.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement