【问题标题】:Problems updating user profiles更新用户配置文件时出现问题
【发布时间】:2009-08-26 14:55:24
【问题描述】:

在编辑配置文件时遇到了一点麻烦!以下代码成功更新了用户配置文件中的 mug_shot 列,但也删除了该特定记录的所有其他列数据。这很奇怪,因为 Django 应该自动区分更新/保存。更奇怪的是,其他任何地方的更新/保存似乎都可以正常工作。

我有点不知所措。

@login_required
def add_mugshot(request):
    user = request.user
    profile = UserProfile.objects.get(user=user)
    if request.method == 'POST':
        profile_form = ProfileForm(request.POST, request.FILES, instance=profile)
        if profile_form.is_valid():
            new_profile = profile_form.save(commit=False)
            new_profile.user = user
            new_profile.save()

            return HttpResponseRedirect('/accounts/profile/')
    else:
        profile_form = ProfileForm(instance=profile)

    return render_to_response('accounts/add_mugshot.html', 
        RequestContext(request, {
            'profile_form': profile_form}))

【问题讨论】:

    标签: django django-models django-profiles


    【解决方案1】:

    我想知道这是否是您的表单模板中的内容。如果您没有显示所有字段,Django 会将它们解释为空,并使用空字段保存您的实例。

    【讨论】:

    • 这是我能想到的最好的解释,但是,我的 edit_profile(request) 代码一切正常。我可以更新个人资料,而不会消失照片列。但是,如果我更新面部照片,其余的记录数据就会消失。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    相关资源
    最近更新 更多