プロファイルの編集に取り組んでいて、ちょっとしたトラブルに遭遇しました! 次のコードは、ユーザー プロファイルの 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}))