Home > Software design >  The concatenation returns me the name of the variable
The concatenation returns me the name of the variable

Time:08-07

I want to create an alert to delete users with sweetAlert, but in the script tag when I create my url from a variable passed as a parameter in the function, the result is only the name of the variable to display and not its value

function delInscPart(id){
 
            var url = "{% url 'suppPartners' "  id " %}"
 
            Swal.fire({
                "title":"Etes vous sure de vouloir supprimé l   invité ?",
                "text":"Si vous confirmer cette opération, Vous supprimerais cette invité !",
                "icon":"",
                "showCancelButton":true,
                "cancelButtonText":"Anuller",
                "confirmButtonText":"Je confirme",
                "reverseButtons":true,
 
 
            }).then(function(result){
                if(result.isConfirmed){
                    window.location.href = url
                    console.log(url)
                }
            })
        }
<td><a href= "#"  onClick="delInscPart('{{list.user_inscrit.username}}');"><i data-feather="trash-2"></i>Supprimer</a></td>

the result is {% url 'suppPartners' id %} instead {% url 'suppPartners' admin %}

CodePudding user response:

The problem is that your template url is never going to dynamically receive the parameter in your javascript function. The template is rendered by the server and produces this:

var url = "{% url 'suppPartners' "  id " %}"
// which becomes something like
var url = "/suppParnters/ id /"

So your url variable will never get updated by the javascript function because 'id' is within the template url that is rendered within the template. Instead you need something like this, that calls the url but preserves the JS parameter:

var url = {% url 'suppPartners' %}   id
// which becomes
var url = "/suppParnters/"   id
  • Related