更新用户配置文件时出现问题

发布于 2024-08-02 22:21:22 字数 822 浏览 0 评论 0原文

正在编辑配置文件时遇到了一点麻烦!以下代码成功更新了用户配置文件中的 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}))

Working out editing profiles and ran into a little trouble! The following piece of code succesfully updates the mug_shot column in the user profile, but also erases all the other column data for that particular record. It's weird because Django is supposed to automatically distinguish between updates/saves. What's weirder is that everywhere else updates/saves seem to work fine.

I'm kind of at a loss.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汹涌人海 2024-08-09 22:21:22

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

I wonder if this is something in your form template. If you aren't showing all of the fields, Django will interpret them as empty, and save your instance with empty fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文