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.

2
  • 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 %}. Commented Apr 20, 2022 at 16:26
  • Check out this one: stackoverflow.com/questions/11333824/… Commented May 10, 2022 at 21:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.