django-tables2: create a input text box
-
26-12-2019 - |
Question
I would like to create a table with multiple columns that display existing data, and one column that contains a text box where I can input data.
When I use my "Animal" model in my forms.py, I see entry boxes. However, when I use the same model in my tables.py, I see a '--' (dashes) instead of the entry boxes.
My models.py:
class Animal(models.Model):
specie = models.CharField(max_length=20)
count = models.CharField(max_length=20)
my forms.py:
class AnimalForm(forms.ModelForm):
class Meta:
model = Animal
my tables.py:
class AnimalTable(tables.Table):
class Meta:
model = Animal
I even tried to manually specify a textbox, but that didn't help:
class AnimalTable(tables.Table):
count = forms.Textarea()
class Meta:
model = Animal
How would one create a entry box inside a django-tables2 table?
Solution
I found a usable solution here.
TEMPLATE = """
<input id="count" maxlength="100" name="count" type="text"/>
"""
class AnimalTable(tables.Table):
count = tables.TemplateColumn(TEMPLATE)
class Meta:
model = Animal
I hope this will prove helpful for others as well.
OTHER TIPS
I think you might have misunderstood the concept of the django-tables2
package. Its purpose is to display tabular data with the ability to sort them and some other formatting stuff. So you just cant add formfields into tables.Table
class, because it expects columns.
The list of supported columns is available in the documentation here: http://django-tables2.readthedocs.org/en/latest/#built-in-columns