Как установить DTL(Django Tamplate language) для цикла и значения через javascript

Я хочу настроить цикл django for loop на установку значения через javascript следующим образом. это правильный способ? или есть какой-нибудь другой способ?

    var table = $('.checkbox-datatable').DataTable({
        'scrollCollapse': true,
        'autoWidth': false,
        'responsive': true,
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "language": {
            "info": "_START_-_END of TOTAL_ entries",
            searchPlaceholder: "Search",
            paginate: {
                next: '<i class="ion-chevron-right"></i>',
                previous: '<i class="ion-chevron-left"></i>'  
            }
        },
        'columnDefs': [{
            'targets': 0,
            'searchable': false,
            'orderable': false,
            'className': 'dt-body-center',
            'render': function (data, type, full, meta){
                return '<div class="dt-checkbox"><input type="checkbox" name=

"id[]" id="chkuser"  value="' +  {% for dd in departmentData %}{{dd.id}}{% endfor %} + '" /><span class="dt-checkbox-label"></span></div>';
        }
    }],
    'order': [[1, 'asc']]
});

urls.py:

urlpatterns = [

    path('get_department_data/', views.get_department_data, name = "get_department_data")
    # ...

]

просмотров:

def get_department_data(request):
    if request.is_ajax and request.method == "GET":
        q = YourModel.objects.all().values()
        data = []
        for i in q:
            data.append([(i['field1'],i['field2'])])
        return JsonResponse({"data":data}, status = 200)

HTML:

<input value="{% url 'get_department_data' %}" type="hidden"></input>

js:

dd_url = $('#department_data_url').val();
$.ajax({
    url: dd_url,
    type: "GET",
    dataType: "json",
    success: function(response) {
        console.log(response.data); 
        $.each(response.data, function (i, obj) {
              console.log( obj[0][0] , obj[0][1] ); 
        });
    }
});
Вернуться на верх