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

219
Views
How to Include JavaScript variable in Django URL in template?

I want to include mangaid variable in "{% url 'manga' mangaid %}". Neither ${mangaid} nor mangaid works. How can I do this?

   let input = document.querySelector("#search")
    input.addEventListener("keyup", () => {
      document.querySelector("#results").innerHTML = "";
      let value = (input.value)
      fetch(`searchresponse/${value}`).then((response) => response.json()).then((results) => {
        results.forEach(function (result) {
          let mangaid = (result["id"])
          document.querySelector("#results").insertAdjacentHTML('beforeend', `<a class="result" id="result${mangaid}">${result["title"]}</a>`);
          document.getElementById("result"+mangaid).addEventListener("click", (event) => {
            event.target.href = "{% url 'manga' mangaid %}"
          })
        })
      })
    })
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can do this by using get params f.e. http://localhost:8000/manga/?a=1.
In the view you can access get params via request.GET.get('a'), in this case the code would return 1 from a=1 in the url.

In JS with djangos url templatetag you can do fetch("{% url 'manga' %}/mangaid=" mangaid)....
Django will replace {% url 'manga' %} with the url named manga.
mangaid needs to be a js variable.

Your view head should look similiar to this:

def mangaView(request):
    mangaid = request.Get.get('mangaid')
    print(mangaid)

EDIT: Url should have trailing / to prevent redirects (301), this reduces server load (important in prod) http://localhost:8000/abc/?a=1.

about 4 years ago · Santiago Trujillo 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!