0

I can send my model to the html page and bring it to the array structure, but I cannot integrate this array into the grid table. Does anyone know how I can do this?

views.py

def tests(request):
    resultQueryet = ResultsModel.objects.all()
    requests = serialize('json', resultQueryet)
    
    context = {
        "requests" : requests
    }
    
    return render(request, 'admin/tests.html', context=context)

html

{% block content %}
    <div class="card-body">
      <div id="table-card" class="table-card"></div>
     </div>
{% endblock %}

    {% block extra_js %}
        <script>
            var model = {{requests|safe}};
            console.log(model)
            document.getElementById("table-card") &&
            new gridjs.Grid({
                columns: ["Name", "Surname", "Date", "Amount", "Result", "Details"],
                sort: !0,
                pagination: { limit: 5 },
                data: [
                    [
                        {% for x in model %}
                            {{x.name}},
                            {{x.surname}},
                            {{x.date}},
                            {{x.amount}},
                            {{x.result}},
                            {{x.details}},
                            
                        {% endfor %}
                    ],
                    
                ],
                }).render(document.getElementById("table-card"))
        </script>
        <script src="{% static 'libs/prismjs/prism.js'%}"></script>
        <script src="{% static 'libs/gridjs/dist/gridjs.umd.js'%}"></script>
        <script src="{% static 'js/pages/gridjs.init.js'%}"></script>
    {% endblock extra_js %}

I can view the model as array on the console screen, but I cannot print it to the table via a for loop.

Elfo
  • 117
  • 1
  • 8
  • I'm not sure why you are serializing the queryset, but you could just pass in the queryset and loop through it. Put this into the context, `'results': ResultsModel.objects.all()`, then in your template's script, `{% for x in results %}`. – raphael Apr 20 '22 at 16:26
  • Check out this one: https://stackoverflow.com/questions/11333824/django-for-loop-in-javascript – user19089099 May 10 '22 at 21:35

0 Answers0