0

I have the following script:

jQuery(document).ready(function($) {
    $( "#continue" ).click(function() {
        var selected = $("#meds").bootgrid("getSelectedRows");
        window.location = "{% url 'meds:prescription' selected %}"
    });
});

and this 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 this url:

url(r'^prescription/(?P<selected>.*)/$', views.PrescriptionView.as_view(), name='prescription')

Knowing that selected is an array, for example [3, 6, 4] I'm trying to use it to display the objects with the id in that array but for some reason even when the array is full nothing is passed in the url, it just looks like this http://127.0.0.1:8000/prescription// with an empty page, like the argument didn't pass

1 Answer 1

1

That's because selected variable is parsed as a Django template variable, but actually it's not. It's a JS variable and, thus, it's parsed as an empty string.

There is a workaround, though:

jQuery(document).ready(function($) {
    $("#continue").click(function() {
        var selected = $("#meds").bootgrid("getSelectedRows");
        var url = "{% url 'meds:prescription' 'test' %}";  // 'test' is just a placeholder value
        url = url.replace('test', selected);  // replace 'test' with the 'selected' value
        window.location = url;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.