2011-08-17 164 views
1

爲什麼我不能在我的未綁定模型表單中設置instance = None設置modelForm實例的初始值

def sample(request): 

    prf_form = ProfileForm(instance=None) 
    print prf_form.instance #debugger print 
    return render_to_response(template, locals(), context_instance=RequestContext(request)) 

我得到以下異常:DoesNotExist

回答

2

另一種方法是實例化一個空的配置文件和傳遞:

profile = Profile() 
prf_form = ProfileForm(instance=profile) 
1

只是不發送它的一個實例:

def sample(request): 

    prf_form = ProfileForm() 
    return render_to_response(template, locals(), context_instance=RequestContext(request))