0

In my django-view I use pandas to create a pivot table which is then handed via context to the template. This works just fine. I want the headers of the columns to be links triggering new views. So I wrote a simple jQuery script to do this. But I run into problems as soon as I want to pass a kwarg with the url template tag..

  $(document).ready(function(){
    $('thead th').each(function(){
      $(this).html('<a href="{% url "cat_view" cat="' + $(this).html() +'" %}">'+ $(this).html() +'</a>');
    });
  });

I get the following error:

Reverse for 'cat_view' with arguments '()' and keyword arguments '{'cat': "' + $(this).html() +'"}' not found. 1 pattern(s) tried: ['category/(?P[\w-]+)/$']

my url entry looks like this:

    url(r'category/(?P<cat>[\w-]+)/$', categoryView, name='cat_view'),

Am I making an error with the syntax or is it a general problem with my approach? The error message suggests, that django interprets what I write in the jQuery script instead of what the jQuery is supposed to pass over to django.. but I don't know how to avoid this..

1 Answer 1

1

Yes, the problem is that Django renders the URLs before the JQuery code is interpreted. I would use a GET parameter instead :

$(document).ready(function(){
    $('thead th').each(function(){
      var html = $(this).html();
      var url = "{% url "cat_view" %}?cat=" + html;
      $(this).html('<a href="' + url + '">'+ html +'</a>');
    });
});
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.