1

How to call django function using ajax with id (or pk).

 $.ajax({
    url: '{% url  app:order_edit %}',
    type:"POST",
        async:false,
    data: {order:order, product:product, customer:customer, qty:qty, uom:uom, des:des},
      success:function(data){
      }
});

views.py

def order_edit(request, obj_id):
    print("Obj Id is:",obj_id)
    if request.method == 'POST':
        form = OrderForm(request.POST)
        if form.is_valid():
            #operations
            return render(request, 'order_view.html', {'form': form})
        return render(request, 'order_view.html', {'form': form})
    return render(request, 'order_view.html', {})

urls.py

 path('orderedit/<int:obj_id>/', views.order_edit, name="order_edit"),

how to call this function Anyone Help me in this issu. Thanks in advance

2 Answers 2

0

You can add parameters to your URL template tag, like this:

  • {% url 'some-url-name' arg1=v1 arg2=v2 %} - keyword syntax
  • {% url 'some-url-name' v1 v2 %} - without keywords

You would have just one parameter:

  • {% url app:order_edit pk %} (delete the space)

where you need to replace pk with the value you want to have in your URL. The rest of your code is not visible, so I do not know from where you can obtain it.

More info on Django documentation: https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#url

Sign up to request clarification or add additional context in comments.

Comments

0

You can only use Django template tags inside JavaScript if your JS is inline. See this answer for info.

If you're doing that, then Tomislav's answer should suffice. Otherwise, you'll also need to think about extracting the URL from your HTML. You could use JS selectors, or something like data- attributes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.