RelatedObjectDoesNOTExist-Edit profile

老子叫甜甜 提交于 2019-12-19 11:33:16

问题


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)


回答1:


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.




回答2:


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})



回答3:


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



来源:https://stackoverflow.com/questions/42973727/relatedobjectdoesnotexist-edit-profile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!