我有一个隐私表格,我在其中选择在访问用户个人资料时应该隐藏哪些应用程序。表单包含几个复选框,用户检查他想要隐藏的内容。我想要的是,当用户访问此表单时,该表单将成为已保存的隐私表单的一个实例,如果它存在的话。我的意思是,如果我已经检查了隐藏应用程序 1,当我再次访问表单时,要检查相应的复选框。
我的代码:
def save_privacy(request):
if request.method == 'POST':
try:
u = Privacy.objects.get(user_privacy = request.user)
form = PrivacyForm(request.POST, instance=u )
except ObjectDoesNotExist:
form = PrivacyForm(request.POST, request.FILES)
if form.is_valid():
new_obj = form.save(commit=False)
new_obj.user_privacy = request.user
new_obj.save()
return HttpResponseRedirect('/accounts/private_profile/')
else:
form = PrivacyForm()
return render_to_response('privacy/set_privacy.html', {
'form': form,
},
context_instance=RequestContext(request))
和我的表格:
class PrivacyForm(ModelForm):
class Meta:
model = Privacy
fields = ['restrict_cv','restrict_blog','friends_of_friends','restrict_followers','restrict_following']