python - 相关对象不存在于/个人资料

标签 python django python-3.x django-models django-forms

我已经创建了用户配置文件模型。我将模型迁移到与数据库同步。但是,当我路由到 /profile (其 url 是此 url(r'^profile$',views.update_profile, name="profile"),) 应该调用 update_profile View ,在检查它是否是 POST 方法之前,我已经在其中打印了 request 对象,但它也没有显示在壳。我哪里做错了?

这是我的代码

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(max_length=600, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(blank=True, null=True)


def create_user_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)
post_save.connect(create_user_profile, sender=User)


def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()
post_save.connect(save_user_profile, sender=User)


class UserForm(forms.ModelForm):

    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'email')


class ProfileForm(forms.ModelForm):

    class Meta:
        model = Profile
        exclude = ('user', )


def update_profile(request):
    print('user ################', request.user, request.user.profile)
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(
            request.POST or None, instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(
                request, ('your profile was successfully updated!'))
            return redirect('products:profile')
        else:
            messages.error(
                request, ('There was an error updating your profile'))
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)
    return render(request, 'dashboard/company/profile.html', {'user_form': user_form, 'profile_form': profile_form})

最佳答案

抱歉,我在发布问题后就发现了结果。如果我需要删除问题,我一定会删除它,否则这里是解决方案

def update_profile(request):
    try:
        profile = request.user.profile
    except Profile.DoesNotExist:
        profile = Profile(user=request.user)
    if request.method == 'POST':
        user_form = UserForm(request.POST, instance=request.user)
        profile_form = ProfileForm(
            request.POST or None, instance=profile)
        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(
                request, ('your profile was successfully updated!'))
            return redirect('products:profile')
        else:
            messages.error(
                request, ('There was an error updating your profile'))
    else:
        user_form = UserForm(instance=request.user)
        profile_form = ProfileForm(instance=request.user.profile)
    return render(request, 'dashboard/company/profile.html', {'user_form': user_form, 'profile_form': profile_form})

上面代码中更新的部分是

try:
        profile = request.user.profile
    except Profile.DoesNotExist:
        profile = Profile(user=request.user)

我无法清楚地解释,但查看错误,配置文件中的RelatedObjectDoesNotexist给了我一个想法来检查request.user.profile是否存在,所以我尝试了这种方法,它对我有用。如果有人对此很了解,请随时详细解释。

关于python - 相关对象不存在于/个人资料,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46291058/

相关文章:

django - 如何从 TabularInline 管理字段中删除添加和更改按钮?

python - 在 python 正则表达式中,为什么 (h)* 和 (h)+ 不能产生相同的结果?

python - 多元时间序列的 LSTM 输入形状?

python - 在组合网络的子网上使用两种损失

Python 解释器无法处理导入的模块?

python - 在 Django 中监控用户事件

python - 下载文件之前如何从链接获取文件名?

python - 管理此 python 异常 : Can't continue with the next iteration/element? 时出现问题

python float 到 in int 转换

python - Python 中的 OpenID Connect 提供程序