将数据绑定到模板或视图中的表单

时间:2017-01-08 03:56:54

标签: django forms formsets

我创建了一个表单:

class KeyForm(ModelForm):
    class Meta:
        model = Key
        fields = ['name', 'access_level', 'permission']

以下是呈现表单的视图:

def key(request):
    template = 'keys.html'
    user = request.user
    key = get_object_or_404(Key, user=user)
    if key.access_level == "0":
        owners = Key.objects.filter(access_level = "0")
        ownersFormSet = formset_factory(KeyForm)
        context = {
            'owners': owners,
            'ownersFormSet': ownersFormSet,
        }
    return render(request, template, context)

我想将'owner'查询集中的数据绑定到'ownersFormSet'。我在模板中迭代'所有者',所以我可以在模板的每次迭代中绑定表单数据吗?

1 个答案:

答案 0 :(得分:0)