H
H
howuu2019-04-07 19:28:28
JavaScript
howuu, 2019-04-07 19:28:28

What is the correct way to pass arguments to Django views using ajax?

I want to make an ajax request to a url that corresponds to a function that accepts (request and pk)

I understand that it is possible to collect such a date and then decompose it into a view, in something like

data = {'csrfmiddlewaretoken':  csrftoken, pk: pk };


and then But before that, I wanted to see how django renders request.POST if we have such a url
pk = int(request.POST['pk'])

url(r'^some/(?P<pk>\d+)/add/$', views.some_add, name='some_add'),

and Django displays csrf_token when outputting request.POST and pk when outputting pk separately.
It turns out that if we make a request via ajax and put pk and token in data, then the url can be made like this

url(r'^some_add/$', views.some_add, name='some_add'),


and if we need to work with pk, then get it like this . But is it correct to make such urls, is it safe? if not right, then how is it right?
pk = int(request.POST['pk'])

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question