Passing objects from Django to Javascript DOM

前端 未结 14 1116
野的像风
野的像风 2020-12-04 21:26

I\'m trying to pass a Query Set from Django to a template with javascript.

I\'ve tried different approaches to solve this:

1. Normal Approach - Javas

14条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 22:03

    For me to send the whole QuerySet (while preserving the fields names; sending object not list). I used the following

        # views.py        
        units = Unit.objects.all()
        units_serialized = serializers.serialize('json', units)
        context['units'] = units_serialized
    

    and just use safe tag in the template

        # template.html
        
    

提交回复
热议问题