Search code examples
pythondjangopython-2.7django-tables2

django-tables2: create a input text box


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.