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

185
Views
use django "include" inside a js function

I am doing a search page in which parameters are sent by ajax and then upon reception of the queryset I rebuild my cards. The whole thing is classic and working ok, here is a simplified version of the thing. Lots of lines killed or modified since it is not really the subject of the post

let getobject = async (value,url) => {
    var res2 = await fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            "X-CSRFToken": getCookie("csrftoken"),
        },
        body: JSON.stringify({
            value: value,
        })
    })
    let data2 = await res2.json();
    videoitems.innerHTML = ''
    modalbin.innerHTML = ''
    data2["data"].forEach(async item => {
        if (item.ext == '.mp4') {
            const dynamicreation = async () => {
                let dyncontent3 = await createnewcard(item)
                let placing = await videoitems.appendChild(dyncontent3);
            }
            const nooncares2 = await dynamicreation()
        } else if (item.ext == ".pdf") {
            const dynamicreation2 = async () => {
                let dyncontent4 = await createnewcard(item)
                let placing2 = await videoitems.appendChild(dyncontent4);
            }
            const nooncares4 = dynamicreation2()
        }
    })
}

the createnewcard function

var createnewcard = item => {
    var dyncontent =  document.createElement("div");
      dyncontent.innerHTML = 
      `<div class="m-2 extralarge-modal video${item.id}">
        <div data-reco="${item.id}"
          class="extralarge-modal  bg-white rounded-lg border border-gray-200 shadow-md dark:bg-gray-800 dark:border-gray-700">
          <div class="p-5">
            <p class="mb-3 font-normal text-gray-700 dark:text-gray-400">
              ${item.title}
            </p>
          </div>
        </div>
      </div>`;
      return dyncontent
    }

What I would like to know is if it would be possible to mix this js with the django "include" function and instead of using js template litterals use an html component of the card that I would include upon looping in the data reveived. I could also maybe include it inside the createnewcard js function but so far it all failed quite miserably. Thanks a lot

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Yes, you've to create card.html or whatever you can name then you've to just include inside your js make sure you use same variables while looping in js eg.(item)

card.html

<div class="m-2 extralarge-modal video${item.id}">
  <div data-reco="${item.id}"
          class="extralarge-modal  bg-white rounded-lg border border-gray-200 shadow-md dark:bg-gray-800 dark:border-gray-700">
    <div class="p-5">
       <p class="mb-3 font-normal text-gray-700 dark:text-gray-400">
          ${item.title}
       </p>
    </div>
  </div>
</div>

and inside your Js do like this

var createnewcard = item => {
    var dyncontent =  document.createElement("div");
    dyncontent.innerHTML = `{% include "card.html" %}`
    return dyncontent
}

over 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!