K
K
kicherov_maxim2020-02-20 15:08:52
Django
kicherov_maxim, 2020-02-20 15:08:52

How to correctly make django urls for ajax request?

Good afternoon. There is a js file in which an asynchronous request is executed, it is planned to connect it to different pages.

function calculateFingerprint(){
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'fingerprinter/fingeprint', false);
  xhr.send(fingerprint);
  if (xhr.status != 200) {
    console.log( xhr.status + ': ' + xhr.statusText );
  } else {
    return xhr.responseText;
  }
};


there is a main url
from django.contrib import admin
from django.urls import path, include
from fingerprinter import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('fingerprinter/', include('fingerprinter.urls')),
    path('', views.index, name='index'),
]


fingerprinter applications
urlpatterns = [
    path('', views.index, name='index'),
    path('fingeprint', views.calculate_fingerprint, name='calculate_fingerprint'), #Сюда ajax хочу принимать
]


As you can see from the code, the index page is available at two addresses. / and /fingeprinter/
from the first address everything is fine, but from the second address I get an error:
django-fingerprint.js:247 POST http://localhost:8000/fingerprinter/fingerprinter/fingeprint 404 (Not Found)

If to the line xhr.open('POST', 'fingerprinter/fingeprint', false); change to xhr.open('POST', 'ingeprint', false); it will be vice versa. How to make it always work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2020-02-20
@Realmixer

I won’t tell you what the problem is right off the bat, but I will recommend the battery https://pypi.org/project/django-extensions/ - I have it in every project by default. Connect it, and in addition to any usefulness, you will have the following management command:
manage.py show_urls
I think everything will clear up right away!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question