Q: Ajax in Django : url not found I am coding a small Django project where an user can select an object and save it in a database. I am trying to implement an Ajax call on a button to delete this object if necessary.
I am doing it step by step, debugging with the console.
my urls:
app_name = 'register'
urlpatterns = [
    path('<int:user_id>/', views.account, name='account'),
    path('/delete/', views.delete, name='delete'),
    ]

my view.py:
def delete(request):
   data = {'success': False} 
   if request.method=='POST':
      product = request.POST.get('product')
      print(product)
      data['success'] = True
   return JsonResponse(data)

my ajax.js:
$("#form_id").on('submit', function(event) {
    event.preventDefault();    
    var product = 'coca-cola'
    console.log('ok till this point')
    $.ajax({        
        url: '{% url "register/delete" %}',        
        type: "POST",
        data:{
             'product':product, 
             'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
        },
        datatype:'json',
        success: function(data) {
          if (data['success'])
             console.log('working fine')
        }
    }); 
});

My view isn't doing much for now but I haven't any knowledge about Ajax and I am doing it one step at a time.
This is the error I get in the console:
jquery.min.js:2 POST http://127.0.0.1:8000/register/6/%7B%%20url%20%22register/delete%22%20%%7D 404 (Not Found)

As far as I understand, Ajax can't find my  url: '{% url "register/delete" %}'.
I have tried '{% url "register:delete" %}' with no luck either.

A: I found an answer after some tweaking, I defined my url before the Ajax call and then pass it in it:
$("#form_id").on('submit', function(event) {
    event.preventDefault();    
    var product = 'coca-cola'
    var url = '/register/delete/'
    console.log( url)
    $.ajax({        
        url: url,        
        type: "POST",
        data:{
             'product':product, 
             'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val()
        },
        datatype:'json',
        success: function(data) {
          if (data['success'])
             console.log('working fine')
        }
    }); 
});


A: Also you can add just the string of url to "url" parameter without characters {% url %}. Maybe you copied the code from pattern Django and added it to JS-file. So it does not work.
