0

I want to use a nested for loop to iterate through all the items of my model, but also through the selection of the fields I decide in this model only!

The idea is to display all the items in a data table but only with the fields I have chosen.

Here is what I have tried:

<table class="table table-bordered" id="example" style="text-align: center; font-size: 14px;">
        <thead class="table-success">
            <tr>
                {% for verb in verbose %}
                <th>{{ verb }}</th>
                {% endfor %}
            </tr>
        </thead>

        <tr>
            {% for test in tests %}
                {% for f in fields %}
                <td class="body-table" style="padding: 1px;">{{test}}.{{f}} </td>

                {% endfor %}
                
            {% endfor %}
       </tr>
</table>

I let the error which I think is self explainable {{test}}.{{f}}

Below is the snippet of the view:

def test_home(request):
    verbose = Test().get_all_fields_verbose()
    fields = Test().get_all_fields_names()
    
    test_list = Test.objects.all().order_by('-created_at')
    system1= System0.objects.all()
    form = TestForm()
    items=len(verbose)

    context = {"tests":test_list,"systems":system1, "form":form,"items":items, "verbose":verbose,"fields":fields }
    return render(request, 'test_simple_home.html', context )
  • the syntax is `{{test.f}}` – Karina Klinkevičiūtė Mar 12 '23 at 13:07
  • Could you also post your view? It's not clear now what `test` and `fields` are. But anyway, it's not the correct way to iterate through them. As I understand, `test` is some model? And `fields` are fields on this model? But they are supplied to the model separately? – Karina Klinkevičiūtė Mar 12 '23 at 13:14
  • 1
    Thanks for your comment and Reply and will update to reflect your point – Hello World Mar 12 '23 at 16:02
  • Thanks for adding your view. In this case it's not going to work like that. Depending on how exactly it needs to work, there might be different options. You could use the `values` method of queryset or `values_list`. I will add an answer, it will be easier to explain there. – Karina Klinkevičiūtė Mar 13 '23 at 18:22

2 Answers2

0
<table class="table table-bordered" id="example" style="text-align: center; font-size: 14px;">
<thead class="table-success">
    <tr>
        {% for verb in verbose %}
            <th>{{ verb }}</th>
        {% endfor %}
    </tr>
</thead>

<tbody>
    {% for test in tests %}
        <tr>
            {% for f in fields %}
                <td class="body-table" style="padding: 1px;">{{ test|get_field:f }}</td>
            {% endfor %}
        </tr>
    {% endfor %}
</tbody>
Iqbal Hussain
  • 1,077
  • 1
  • 4
  • 12
0

View:

def test_home(request):
    # verbose = Test().get_all_fields_verbose()
    # fields = Test().get_all_fields_names()
    
    test_list = Test.objects.all().order_by('-created_at').values('field1', 'field2', 'field3')
    system1= System0.objects.all()
    form = TestForm()
    items=len(verbose)

    context = {"tests":test_list,"systems":system1, "form":form,"items":items }
    return render(request, 'test_simple_home.html', context )

Template:

<table class="table table-bordered" id="example" style="text-align: center; font-size: 14px;">
        <thead class="table-success">
            <tr>
                {% for key, value in tests %}
                <th>{{ key }}</th>
                {% endfor %}
            </tr>
        </thead>

            <tr>
                {% for key, value in tests %}
                <td>{{ value }}</td>
                {% endfor %}
            </tr>
</table>

I'm just not sure if it will give verbose names of fields here, or not. But you can try this way, and see how it goes. Check this example for that.