# Create a table in an html page using user generated variables (through admin)

**URL:** <https://forum.djangoproject.com/t/create-a-table-in-an-html-page-using-user-generated-variables-through-admin/18816>\
**Category:** Templates & Frontend\
**Created:** [February 13, 2023, 2:20pm UTC](https://forum.djangoproject.com/t/create-a-table-in-an-html-page-using-user-generated-variables-through-admin/18816 "2023-02-13T14:20:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![NaClPhantom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/naclphantom/32/11650_2.png) [@NaClPhantom](https://forum.djangoproject.com/u/NaClPhantom)\
**Post date:** [February 13, 2023, 2:20pm UTC](https://forum.djangoproject.com/t/create-a-table-in-an-html-page-using-user-generated-variables-through-admin/18816/1 "2023-02-13T14:20:47Z")

</div>

I want to make a table in the html page so that every time the admins input a new object sub\_category (a predetermined class) it will update the table.

For reference im trying to make something like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/1/122b92d53aed6727736c11e2d1e103abfd881386.png)

Here is my code, thank you in advanced!

HTML page that displays the table:

```auto
{% extends 'item_type/base.html' %}

{% block content %}

<h1>Clothing</h1>

    <table>
        
            
                {% for j in sub_category %}
                    {% with x=0 %}
                        <tr>

                            {% while x<4 %}
                                <td> <a href="{% url 'show_subcategory' j.id %}"> {{j}}</td>
                                
                            {% endwhile %}
                            
                        </tr>
                        <tr>
                            {% while x>4 and x<8%}
                                <td> <a href="{% url 'show_subcategory' j.id %}"> {{j}}</td>
                                {% x+=1%}
                            {% endwhile %}
                        </tr>
                        {% x+=1%}
                    {% endwith %}
                {% endfor %}
        

    </table>

       
{% endblock %}

```

Class where sub categories are created (ignore the spelling of category!)

```auto
class SubCategorie(models.Model):
    sub_category = models.CharField('Clothing Type',max_length=100)
    def __str__ (self):
        return self.sub_category

```

Function in views.py thats linked to the html page

```auto
def clothing(request):
    sub_category = SubCategorie.objects.all()
    rows = [1,2,3]
    columns = [1,2,3,4]
    context = {'sub_category':sub_category,'rows':rows,'columns':columns}
    return render(request,'item_type/clothing.html',context )

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 13, 2023, 3:21pm UTC](https://forum.djangoproject.com/t/create-a-table-in-an-html-page-using-user-generated-variables-through-admin/18816/2 "2023-02-13T15:21:50Z")

</div>

Do you know in advance how many columns you want?  
Do you know how those categories are supposed to be divided into columns?

It’s easy to create this layout by breaking your page organizations into columns using CSS. That way, you can render these elements “linearly”, without needing to “over manage” the column structure.

For example, if you were using bootstrap, it would be something to the general effect of:

```auto
<div class="col">
 <p></p>
 <p></p>
 ...
</div>
<div class="col">
 <p></p>
 <p></p>
 ...
</div>
<div class="col">
 <p></p>
 <p></p>
 ...
</div>

```

This way it’s going to be a whole lot easier to generate than trying to render the data in row-order

---

<div class="post-metadata">

**Author:** ![NaClPhantom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/naclphantom/32/11650_2.png) [@NaClPhantom](https://forum.djangoproject.com/u/NaClPhantom)\
**Post date:** [February 15, 2023, 1:58am UTC](https://forum.djangoproject.com/t/create-a-table-in-an-html-page-using-user-generated-variables-through-admin/18816/3 "2023-02-15T01:58:52Z")

</div>

Thank you very much! with regards to your questions. I wanted it to be separated in 4 columns and what i was wondering if it was possible was that the website updated itself in case a new “sub-category” was added so that it would automatically change the config of the table. Its not fully required for what im trying to do as i can always manually change it but just a personal question. Once again, thanks for the helpful reply!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 15, 2023, 5:00am UTC](https://forum.djangoproject.com/t/create-a-table-in-an-html-page-using-user-generated-variables-through-admin/18816/4 "2023-02-15T05:00:28Z")

</div>

> [@NaClPhantom](#):
>
> i was wondering if it was possible was that the website updated itself in case a new “sub-category” was added so that it would automatically change the config of the table.

It’s a question of _what_ you want changed and whether you can identify that algorithmically. For instance, if you want 4 columns of roughly equal size but you don’t want to split categories across columns, that’s fairly easy. So it’s not so much a question of _if_ it can be done - it almost certainly can. It’s a question of whether you can define _how_ to achieve the desired result.
