Reputation: 21
Django==1.10.5. I have a problem RelatedObjectDoesNotExist Views.py:
def register(request):
if request.method == 'POST':
user_form = UserRegistrationForm(request.POST)
if user_form.is_valid():
# Create a new user object but avoid saving it yet
new_user = user_form.save(commit=False)
# Set the chosen password
new_user.set_password(user_form.cleaned_data['password'])
# Save the User object
new_user.save()
# Create the user profile
profile = Profile.objects.create(user=new_user)
return render(request,
'account/register_done.html',
{'new_user': new_user})
else:
user_form = UserRegistrationForm()
return render(request, 'account/register.html', {'user_form': user_form})
and
@login_required
def edit(request):
if request.method == 'POST':
user_form = UserEditForm(instance=request.user,
data=request.POST)
profile_form = ProfileEditForm(instance=request.user.profile,
data=request.POST,
files=request.FILES)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
else:
user_form = UserEditForm(instance=request.user)
profile_form = ProfileEditForm(instance=request.user.profile)
return render(request, 'account/edit.html', {'user_form': user_form,
'profile_form': profile_form})
The problem is that: profile_form = ProfileEditForm(instance=request.user.profile)
Upvotes: 1
Views: 1079
Reputation: 31
I have similar experience working on this particular exercise. The error is coming right from code below
Profile_form = ProfileForm(request.POST, instance=request.user.profile)
We are trying to get "request.user.profile" not realizing "User" in the model doesn't have a "profile" but a Profile has a"User" . Therefore a better way to get the "profile":
profile = Profile(user=request.user)
Hence your edit method should look close to this:
@login_required
def edit(request):
profile = Profile(user=request.user)
if request.method == 'POST':
user_form = UserEditForm(instance=request.user,data=request.POST)
profile_form = ProfileEditForm(instance=profile,
data=request.POST,
files=request.FILES)
if user_form.is_valid() and profile_form.is_valid():
user_form.save()
profile_form.save()
messages.success(request, 'Profile updated successfully')
else:
messages.success(request, 'Error updating your profile')
user_form = UserEditForm(instance=request.user)
profile_form = ProfileEditForm(instance=profile)
return render(request,
'accounts/edit.html',
{'user_form': user_form,
'profile_form': profile_form})
Upvotes: 3
Reputation: 302
I tried another alternative this scenario is that when we update our code we have some users that registered but not have a profile. I deleted the users and in admin and signed up with account it worked
but you will have a problem when logging with facebook
Upvotes: -2
Reputation: 495
Just to answer this question, this code is a snippet from the Django By Example
book from the project Building a Social Website
.
I encountered the same error when I clicked on the edit your profile
link in the dashboard.
So, to solve this error, if you are logged in as a superuser, then go to the admin site of your project (url: localhost/admin/
) and then under the Account
heading, click on Profiles
and click the Add profile
link on the top right corner. Select the user from the dropdown, add the profile details and click save
.
Now going back to the dashboard and clicking the edit your profile
link should display the desired view.
Upvotes: 2