") row.append("" + str(myform.fields.get(field)) +...">

How do you render a django form in a view?

for field in FIELDS:
    row = []
    row.append("<tr>")
    row.append("<td>" + str(myform.fields.get(field)) + "</td>")
    row.append("</tr>")

    custom_fields.append("".join(row))

When I pass the template custom_fieldsto the template, all I get is:

<tr><td><django.forms.widgets.CheckboxInput object at 0x1fa7d90></td></tr>

How can I get the processed form correctly?


This is what I will do in the end:

form1 = CustomForm1()
form2 = CustomForm2()
form3 = CustomForm3()

for field in FIELDS:
    row = []
    row.append("<tr>")
    row.append("<td>" + str(form1.fields.get(field)) + "</td>")
    row.append("<td>" + str(form2.fields.get(field)) + "</td>")
    row.append("<td>" + str(form3.fields.get(field)) + "</td>")
    row.append("</tr>")

    custom_fields.append("".join(row))

Thus, I can display all form fields together in one table.

+3
source share
3 answers

I just figured it out. It is form[field]insteadform.fields[field]

>>> f = MyForm()
>>> f
<myform.forms.MyForm object at 0x1fa7810>

>>> f['myfield']
<django.forms.forms.BoundField object at 0x20c7e50>

>>> f.fields['myfield']
<django.forms.fields.BooleanField object at 0x1fa7850>

therefore form.fields, it is a list of all unrelated fields, and form.__getitem__a callable that returns the associated fields.

+8
source

, , as_table, as_ul, as_p, .

as_table, , , , . django.forms.forms.BaseForm - .

+4

Do you pass the list of fields to the template yourself? Fields must be part of an object derived from forms.Formso that the template displays it correctly.

Given that you have a call myform.fields.get(), it looks like you have a Form object; what exactly are you trying to do with this list of fields?

0
source

Source: https://habr.com/ru/post/1715288/


All Articles