Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

507
Views
How to use javascript variable in django if statement (template)?

I'm trying to update my html element with django if statement:

element.innerHTML= `{% if person.name == ${value} %} something {% endif %}`

but I receive an error:

TemplateSyntaxError at /
Could not parse the remainder: '${value}' from '${value}'

I also tried:

 `{% if person.name == ${value} %} something {% endif %}`

But the statement doesn't work properly. Is there anything I can do to combine javascript with Django variables?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Short Answer
No. You can not do this, at least not that easily.

Detailed (slightly) Answer
Django replaces all tags, {{ }} and {% %} with the result at the server (back end) end when the page is rendered, before it gets to the client. Your JavaScript (JQuery?) variable is evaluated by the client (the front end). Django will not know what ${value} is. So, no, you can not use a JavaScript variable in Django template code.

This does not mean you can not achieve what you want, but it must be done in the following way. Your JavaScript code can fetch the value of person.name from a view and then you can do your if statement in the JavaScript file after the response from the fetch is received. The view could return the value of person.name as a JsonResponse

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!