0

I have a script like this for adding the data of the table from a JSON.

<script>


    const tableData = $('#table-data').DataTable({
            dom: 'Bfrtip',
            "serverSide": true,
            "processing": true,
            "ajax": {
                "url": '{{ data_url }}'
            },
            "columns": [
                {
                    {% comment %} "data": "pertanyaan__bidder_id__name" {% endcomment %}
                    "data": "pertanyaan__bidder_id__name"
                },
                {"data": "waktu_pengambilan"
                 },
                {"data": "id",
                    "render": function(data, type, full, meta){
                        const statusUrl = "{{ status_url }}".replace('1', data)
                        const actions = []
                        const dokumen = data
                        actions.push(`<a href="${statusUrl}" class="btn btn-sm btn-dark" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-custom-class="tooltip-dark" title="Download" target="_blank" style="pointer-events: none;">Lihat<span class="btn-icon-end"><i class="fa fa-eye"></i></span></a>`)
                        return actions.join('&nbsp;') 
                    }
                },
                {"data": "keterangan"},
            ]
    })

    $('.dt-button').addClass('btn btn-primary')
    $('.dt-button').removeClass('dt-button')

</script>

the JSON gets from class PertanyaanData. This result will pass to a class called PertanyaanView

class PertanyaanData(View):
    def get(self, request):
        limit = int(request.GET.get("length", 10))
        offset = int(request.GET.get("start", 0))
        rows = ListDokselPertanyaan.objects.all()[offset:limit].values( 'id','pertanyaan__bidder_id__name', 'waktu_pengambilan', 'pertanyaan__pertanyaan', 'keterangan', 'pertanyaan')
        count = rows.count()

        data = {
            'draw': request.GET.get("draw", 1),
            'recordsTotal': count,
            'recordsFiltered': count,
            'data': list(rows),
        }
        return JsonResponse(data)
class PertanyaanView(View):
    def get(self, request):
        context = {
            'title': 'Pertanyaan Dokumen Seleksi',
            'data_url': reverse_lazy(conf.APP_BASE_NAME + conf.PERTANYAAN_DATA_URL_NAME),
            'status_url': reverse_lazy(conf.APP_BASE_NAME + conf.PERTANYAAN_SELECT_URL_NAME, kwargs={'pk': 1})
        }
        return render(request, 'app/pertanyaan/index.html', context)

I am a beginner in Django and I don't know how to make an if statement here. For example, I want to make 2 conditions for the button depends the value of "pertanyaan__pertanyaan" in JSON. If the value of "pertanyaan__pertanyaan" is not null I want to make a clickable button, and if it's null I want to make a non-clickable button.

fathimah
  • 31
  • 5

1 Answers1

1

You can make if statements in django using {% and %}. Here are some examples :

{% if ... %}
...
{& elif ... %}
...
{% else %}
...
{% endif %}

Note the presence of the {% endif %} at the end which is mandatory.

If you want to check if pertanyaan__pertanyaan is null or not, the following might work :

{% if pertanyaan__pertanyaan %}
...
{% else %}
...
{% endif %}

You can also do loops and other interesting stuff. Here is the doc if you want more infos.

Hope this helps !