/ profile上的RelatedObjectDoesNotExist

时间:2017-09-19 02:56:48

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

我创建了用户个人资料模型。我将模型迁移到与数据库同步。但是,当我路由到User has no profile时,我会收到/profile (whose url is this url(r'^profile$', views.update_profile, name="profile"),)的错误,该update_profile应该调用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}) 对象之前检查它是POST方法还是不是,但它也没有在shell中显示。我哪里做错了?

这是我的代码

<h1>Add Post</h1>
<% form_for :post do |f| %>
    <p>
        <%= f.label :title %><br>
        <%= f.text_field :title %>
    </p>


    <p>
        <%= f.label :body %><br>
        <%= f.text_area :body %>
    </p>


    <p>
        <%= f.submit %>
    </p>
<% end %>

2 个答案:

答案 0 :(得分:2)

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

的解决方案
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是否存在,所以我尝试了这种方式,它对我有用。如果有人知道这一点,请随时详细解释。

答案 1 :(得分:0)

你可以用较小的代码编写,比如

def update_profile(request):
    profile = Profile.objects.get_or_create(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=profile)
    return render(request, 'dashboard/company/profile.html', {'user_form': user_form, 'profile_form': profile_form})