Im正在创建简单的动态form fields
,用户可以添加他们的表单字段,并且所有字段都与category
模型相关。我希望当用户选择名为car
的category
,然后向他们显示与该车相关的fields
。在
我的结构:
class Category:
name = ...
class Field:
label = ...
category = ForeignKey(Category)
class FieldValue:
value = ...
field = ForeignKey(Field)
我的问题是如何生成我的表单以及如何从form.cleaned_data
中检索数据,从而可以将记录添加到FieldValue
模型中。我创建了一个表单,它可以很好地使用__init__
进行渲染。我想从呈现的表单字段中检索数据。在
我的表格:
^{pr2}$我的观点:
def some_create_view(request, category_id):
if request.method == 'POST':
form = CategoryFieldsForm(category_id)
form.save()
form = CategoryFieldsForm(category_id)
return render(request, 'create.html', {'form': form})
当我提交表单时,CategoryFieldsForm
对象没有属性cleaned_data
显示。在
你可以读更多关于表格有效吗()here。在
您需要用POST数据实例化表单,然后调用
form.is_valid()
,然后才能访问form.cleaned_data
。在感谢Daniel Roseman和其他人。我认为这是一个更简单的错误。在
我只是忘了把
request.POST
传给我的表格。在相关问题 更多 >
编程相关推荐