Search code examples
pythondjangotemplatesfilterdjango-context

django form_data filter template


I have 4 class models, and i have done a WizardView.

In the end of the view, I have

return render_to_response('fattura_wizard.html', {
    'form_data': [form.cleaned_data for form in form_list],
})

I need to use the single fields in the HTML (without a for), I have tried to do a filter, but I don't understand if it is possibile with the following {{ form_data }}:

[
{'FormField1': u'field4', 'FormField2': u'field2', 'FormField3': u'field3', 'FormField4': u'field1', 'FormField5': u'fi'},
{'FormField6': u'', 'FormField7': u'', 'FormField8': u'field6', 'FormField9': u'field5', 'FormField10': u'', 'FormField11': u'', 'FormField12': u'', 'FormField13': u'', 'FormField14': u'', 'FormField15': u'', 'FormField16': u'', 'FormField17': u'', 'FormField18': u''},
{'FormField19': u'', 'FormField20': u'', 'FormField21': u'', 'FormField22': u'', 'FormField23': u'', 'FormField24': u'', 'FormField25': u'', 'FormField26': u''},
{'FormField27': datetime.date(2015, 8, 25), 'FormField28': datetime.date(2015, 8, 25), 'FormField29': u'', 'FormField30': datetime.date(2015, 8, 25)}
] 

my try was around:

@register.filter(name='lookup')
def cut(value, arg):
    return value[arg]

{{ mydict|lookup:item.name }}

I've tried passing in the return something like value[key1][key2] without any result.

PS there are a lot of blank spaces because I've compiled only the first of 4 forms.


Solution

  • The OP wrote:

    solved with a simple_tag:

    @register.simple_tag
    def keyval(fdata, parameter, stindex, ndindex=None):
    if(ndindex is None):
        return fdata[stindex][parameter] 
    return fdata[stindex][ndindex][parameter]
    

    and I call it in template with:

     {% keyval form_data "FormField1" 0 %}