I've created a user-signup page which adds users to User Model in Django
But when the data is saved, the Password is not encrypted, i.e. stored as Text only. This creates a problem when the user tries to log in (as Django tries to Decrypt the original password, and they both don't match).
Also, I'm extending the User Model so as to add more information about the users which is saved in the Profile Model (Using a One-To-One Link)
views.py
def user_signup(request):
if request.method == "POST":
user_form = userSignup(request.POST)
phone = request.POST['phone']
address = request.POST['address']
pincode = request.POST['pincode']
if user_form.is_valid() :
user = user_form.save()
auth.login(request,user)
userdata = User.objects.all()
for userinfo in userdata:
if userinfo.username == user.username:
user_id=user.id
update_data = Profile.objects.get(pk = user_id)
update_data.address=address
update_data.phone=phone
update_data.pincode=pincode
update_data.save()
return redirect('/')
else:
return HttpResponse(" SIGNUP FAILED")
else:
form = userSignup()
profile_form = userSignup_profile()
return render(request,"user_signup.html",{'form':form, 'profile_form':profile_form})
def user_logout(request):
auth.logout(request)
return redirect('/')
user_signup.html
<body>
<form action="user_signup" method="POST">
{% csrf_token %}
{{form.as_p}}
{{ profile_form.as_p}}
<button class="primary" type="submit" >SIGNUP </button>
</form>
</body>
Models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
phone = models.CharField(max_length=10)
address = models.TextField(max_length=200)
pincode = models.IntegerField()
forms.py
class userSignup(forms.ModelForm):
password = forms.CharField(max_length=50, widget = forms.PasswordInput())
class Meta:
model = User
fields = ('first_name', 'last_name','username', 'password', 'email')
How can I save the new user password in Encrypted form, and not the actual password??
set_password
– Generalist