更新用户配置文件时出现问题
正在编辑配置文件时遇到了一点麻烦!以下代码成功更新了用户配置文件中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想知道您的表单模板中是否有此内容。如果您没有显示所有字段,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.