我正在構建一個創建佈局頁面供用戶輸入值並在模板中使用。我現在的問題是在我的視圖函數中,我無法從模型中獲取字段數據並將其重新填充爲django格式。我只需要爲佈局模型輸入1個數據,該模型始終被抓取並放入表單中,以便用戶可以編輯然後重新保存它。這個問題將存在於View.py中。這裏是我有:如何從模型中的字段中獲取數據並將它們填充爲django表格
View.py:
def create_layoutform(request, template_name='layout/create.html'):
if len(Layout.objects.all()) > 0:
# if you have a form, load it
MyLayout = Layout.objects.get(pk=1)
LayoutUpdateForm = LayoutForm(MyLayout)
tmpl_vars = {
'layoutupdateform':LayoutUpdateForm,
}
return render(request, template_name, tmpl_vars)
else:
layout_form = LayoutForm(request.POST or None)
layout = layout_form.save(commit=False)
layout.save()
tmpl_vars = {
'layoutform':layout_form,
}
return render(request, template_name, tmpl_vars)
我的打印輸出的觀點:Layout.objects.get(PK = 1)
抓鬥只是模型的名稱(佈局對象)
'的第一個參數一個Django表單是數據,但要使用實例參數[1] .'得到它現在的工作,謝謝。 –