简体   繁体   中英

Django shows names instead of objects

I have array of inputs

class ChooseColumnsForm(forms.Form):
    choices = (
        (1, ("")),
        (2, ("sn")),
        (3, ("cn")),
        (4, ("password")),
        (5, ("uid")),
        (4, ("mail"))
    )
    columns = []
    def set_columns(self, col):
        for i in range(0,col):
            self.columns.append(forms.ChoiceField(self.choices))

And i want to show them

{% for choice in columns %}
    <td>{{ choice }}</td>
{% endfor %}

But instead of inputs I am getting names of that inputs.

产量

What i want to get

结果

I know i could just put the same input like thiu:

{% for choice in form_col.columns %}
    <td>{{ form_col.lol }}</td>
{% endfor %}

Where form_col.lol is ChoiceField in forms.py, but then i couldn't get all choices:

print(form.cleaned_data['lol'])

This show only select in last ChoiceField.

Yay, i think i found it.

First of all in form.py i deleted variable columns. I had to add new forms to fields wchich is a array in forms.Form. Also i had to create own render(?) function as_row:

class ChooseColumnsForm(forms.Form):
    choices = (
        (1, ("")),
        (2, ("sn")),
        (3, ("cn")),
        (4, ("password")),
        (5, ("uid")),
        (4, ("mail"))
    )
    def set_columns(self, col):
        for i in range(0,col):
            self.fields['column'+str(i)] = forms.ChoiceField(self.choices)
    def as_row(self):
        return self._html_output(
            normal_row='<td>%(errors)s%(field)s%(help_text)s</td>',
            error_row='<td colspan="2">%s</td>',
            row_ender='</td>',
            help_text_html='<br /><span class="helptext">%s</span>',
            errors_on_separate_row=False)

In views.py i am sending this

form_col = ChooseColumnsForm()
form_col.set_columns(length)
return render(request, 'django_ldap/add_students.html', {'form': form, 'form_col': form_col.as_row(), 'file2': file2})

And in html just

<thead>
    {{ form_col }}
</thead>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM