python - Django - Pass a jQuery array as url argument -
i have following script:
jquery(document).ready(function($) { $( "#continue" ).click(function() { var selected = $("#meds").bootgrid("getselectedrows"); window.location = "{% url 'meds:prescription' selected %}" }); }); and view:
class prescriptionview(generic.listview): template_name = 'meds/prescription.html' context_object_name = 'meds' model = medicament def get_queryset(self): return medicament.objects.filter(id__in=self.kwargs['selected']) with url:
url(r'^prescription/(?p<selected>.*)/$', views.prescriptionview.as_view(), name='prescription') knowing selected array, example [3, 6, 4] i'm trying use display objects id in array reason when array full nothing passed in url, looks http://127.0.0.1:8000/prescription// empty page, argument didn't pass
that's because selected variable parsed django template variable, it's not. it's js variable and, thus, it's parsed empty string.
there workaround, though:
jquery(document).ready(function($) { $("#continue").click(function() { var selected = $("#meds").bootgrid("getselectedrows"); var url = "{% url 'meds:prescription' 'test' %}"; // 'test' placeholder value url = url.replace('test', selected); // replace 'test' 'selected' value window.location = url; }); });
Comments
Post a Comment