I want to show the details in user models and I want to allow users to edit it. I am almost done with it. but instead saving edits it tries to save everything and bringing me a error.
This is the function I used for it :
def EditProfile(request):
if request.method == 'POST':
form = EditProfileForm(request.POST, instance=request.user.userauthentication)
form2 = EditProfileForm2(request.POST, instance=request.user)
username = form2['username'].value()
email = form2['email'].value()
auth = form['D_Auth'].value()
chid = form['D_ChID'].value()
if username is None:
messages.success(request, 'You must enter Username')
return redirect('edit_profile')
elif email is None:
messages.success(request, 'You must enter Email')
return redirect('edit_profile')
elif User.objects.filter(username=username).exists():
messages.success(request, 'Username is already taken')
return redirect('edit_profile')
elif User.objects.filter(email=email).exists():
messages.success(request, 'Email is already Taken')
return redirect('edit_profile')
elif auth is None:
messages.success(request, 'Authentication Key Must be Entered')
return redirect('edit_profile')
elif chid is None:
messages.success(request, 'Channel ID Must be Entered')
return redirect('edit_profile')
elif UserAuthentication.objects.filter(D_Auth=auth).exists():
messages.success(request, 'Authentication key in Use')
return redirect('edit_profile')
elif form.is_valid() and form2.is_valid():
userform = form.save()
customform = form2.save(False)
customform.user = userform
customform.save()
return redirect('Home')
else:
form = EditProfileForm(instance=request.user.userauthentication)
form2 = EditProfileForm2(instance=request.user)
context = {
'form':form2,
'form2':form
}
return render(request, 'registration/edit_profile.html', context)
Yes bro i knew that i can use django forms. But while we talk about frontend, editing the django forms is little more complex, else I need to use bootstrap but i dont want to do those stuffs so i created those forms.
my need:
Now I want to show the current user details in that custom form and I want it to update the user profile instead of over writting it. I wish you will understand me.
You’re using UserAuthentication.objects.update( and User.objects.update( when you I believe you wanted UserAuthentication.objects.update_or_create( and User.objects.update_or_create(.
The entire “form system” as you refer to it is an integrated component of Django. It’s all designed to work together. You can’t just pick and choose the pieces that you think you want to use and ignore the rest without creating a lot of extra work for yourself.