Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
Pass from Javascript to Django a template URL with PK

I implemented Ajax Search with success and results are loaded in a table which is in a template file. My TH are static and I append each result in < tr > < td >... in a foreach loop.

       if (data.length === 0) {
                tableOutput.innerHTML = '<h3>Aucun résultat.</h3>';

            } else {
                nbResults.style.display = "block"
                nbResults.innerHTML = data.length + " produits trouvés"
                data.forEach((item) => {
                    my_url = "{% url 'auto-product-details' ${item.id} %}"

                    if (item.etat__etat == "Echange Standard") {
                        var etat = "ES";
                    } else {
                        var etat = item.etat__etat;
                    }
                    if (item.cau == "0.00") {
                        var cau = "nc";
                    } else {
                        var cau = item.cau;
                    }


                    tableBody.innerHTML += `
                    <tr>
                        <td>${item.sku}</th>
                        <td>${item.etat}</td>
                        <td ${item.nom}</td>
                        <td>${item.sst1}</td>
                        <td>${item.sst2}</td>
                        <td>${item.sst3}</td>
                        <td>${item.pua}</td>
                        <td>${item.cau}</td>
                        <td><a href="/facture/auto-product-details/${item.id}"> 
                        <button type="button" class="btn btn-sm btn-labeled btn- 
                             info">
                        <span style="color: #fff;" class="btn-label"><i class="fas 
                        fa-sign-in-alt"></i></span></a></td>
                    </tr>
                    `;

My problem is I had (above) to "hardcode" my URL "auto-product-details" because when I try to respect Django template syntax like this :

<td><a href="{% url 'auto-product-details' ${item.id} %}">...

but the URL is transformed in

/facture/%7B%%20url%20'auto-product-details'%2036%20%%7D

I tried to build piece by piece my URL in JS, tried to replace the {item.id } after the creation of a base (var) url without PK ... no success. It seems the special characters are transformed in HTML.

Is there a way to achieve what I want to do ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use an intermediate variable:

my_url = "{% url 'auto-product-details' ${item.id} %}"
tableBody.innerHTML += '
    <tr>
        <td>${item.sku}</th>
        <td>${item.etat}</td>
        <td ${item.nom}</td>
        <td>${item.sst1}</td>
        <td>${item.sst2}</td>
        <td>${item.sst3}</td>
        <td>${item.pua}</td>
        <td>${item.cau}</td>
        <td><a href="'+my_url+'"> 
        <button type="button" class="btn btn-sm btn-labeled btn- 
             info">
        <span style="color: #fff;" class="btn-label"><i class="fas 
        fa-sign-in-alt"></i></span></a></td>
    </tr>
    ';
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda