[Django] How to traverse through query set in java script using django templates?

225 views
Skip to first unread message

Dilipkumar Noone

unread,
Dec 6, 2019, 1:42:08 PM12/6/19
to Django users
Hi,

I  have a model named ApplyGSP in models.py.

In my views.py:

apply_gsp_model = ApplyGSP.objects.all()

context = {'form': form,'apply_gsp_model':apply_gsp_model, 'Message': message, 'action': action}
if action == 'edit':
print("action is edit")
return render(request, 'EditApplyGSP.html', context)
else:
print("action is submit")
return render(request, 'ApplyGSP.html', context)

I want to traverse through the model instance in javascript using django template. 

can some one suggest how to traverse through django queryset and access the database fields inside java script.

<script type="text/javascript">
 

</script>

Regards,
N.Dilip kumar.

Andréas Kühne

unread,
Dec 9, 2019, 5:13:32 AM12/9/19
to django...@googlegroups.com
This is a strange way to do things - but all you need to do is to add a template for loop in the javascript part of your template. For example:

<script type="text/javascript">
 {% for gsp in apply_gsp_model %}
   var value1 = {% gsp.id %};
 {% endfor %}
</script>

What will happen then is that the iteration will create that line for each gsp in your apply_gsp_model qs.

Regards,

Andréas


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/789645b6-91f2-40ea-b4c0-44580fb2bdef%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages