4

I want to be able to get data using Jquery then apply a Django template filter on it. I am using jinja2 for the template.

I have a click event as to:

$('#get_name').click(function(event){
    var name = $(this).val();
    alert('{{ name|custom_filter}}'); - here is where it is producing the error.
});

I have tried appending it as '{{ " + name + "|custom_filter}}' but still produces an error.

2
  • Posting the error is always a helpful start. Commented Mar 2, 2016 at 17:18
  • What are you trying to do? Template rendering is done on the server side. Javascript is executed on the client side. Commented Mar 2, 2016 at 17:18

1 Answer 1

3

You cannot mix client-side code and server-side code. You are attempting to apply a template filter to a javascript variable that do not yet exist and is just a piece of text to the template engine. Both scopes are completely different and not even executed on the same machine.

You have to write a javascript function custom_filter (that hopefully does the same thing as the template tag) and just call it:

$('#get_name').click(function(event){
    var name = $(this).val();
    alert(custom_filter(name));
});

Why would you use a template tag for that?

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.