RelatedObjectDoesNOTExist - 编辑个人资料

时间:2017-03-23 10:42:39

标签: django-views

的Django == 1.10.5。我有一个问题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})

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

问题在于:         profile_form = ProfileEditForm(instance = request.user.profile)

3 个答案:

答案 0 :(得分:2)

只是回答这个问题,此代码是来自项目Django By Example的{​​{1}}一书的摘录。

点击信息中心内的Building a Social Website链接时,我遇到了同样的错误。

因此,要解决此错误,如果您以超级用户身份登录,请转到项目的管理站点(网址:edit your profile),然后在localhost/admin/标题下点击Account并点击右上角的Profiles链接。从下拉列表中选择用户,添加个人资料详细信息,然后点击Add profile

现在返回仪表板并单击save链接应显示所需的视图。

答案 1 :(得分:2)

我在这项特殊练习上有相似的经验。 错误来自下面的代码

Profile_form = ProfileForm(request.POST,instance = request.user.profile)

我们正试图获得" request.user.profile"没有意识到"用户"在模型中没有"简介"但是个人资料有一个"用户" 。因此,更好的方式来获得"简介":

        profile = Profile(user=request.user)

因此,您的编辑方法应该与此接近:

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

答案 2 :(得分:-2)

我尝试了另一个替代方案,这个场景是当我们更新代码时,我们有一些注册但没有配置文件的用户。我删除了用户并在管理员中注册了有效的帐户

但使用Facebook登录时会遇到问题