ajax request url syntax error in javascript file

I have a problem with Ajax Request URL syntax in the Javascript File. When the code is in the html file everything is correct.

html file

                    url: '{% url "ajax_load_listanumerowproduktu" %}',   // This syntax   '{%  %}' is a problem
                    data: {
                      'numerproduktu': numerproduktuId
                    success: function (response) {   // jeśli sukces to pobieram to co wysłano przez funkcję load_polepomocnicze
                        console.log('response');      // drukuję do konsoli PRZEGLĄDARKI

                    error: function (){

                        console.log('brak na magzynie takiego numeru');
                        {#alert('brak na magzynie takiego numeru')#}


    path('ajax/load_listanumerowproduktu/', views.load_listanumerowproduktu, name='ajax_load_listanumerowproduktu'),

I know that each file type has it own code interpreter. And for TEMPLATE LANGUAGE I set Django thats why I use {# #} to comment code.
My question is how can I use other syntax for URL in Ajax Request save in Javascript file?
I need figure out it to make my project clear and do not centralize information in long file… I want seperate files…

You typically do not render JavaScript files. The files are sent verbatim by the web server, so you cannot use template tags or filters in your .js files.

You do have a couple options.

First, and the method that we prefer, is to use the json_script tag in the html for data needing to be rendered, and then access that data from the main part of your JavaScript code.

Or, you could use the include tag to include your JavaScript file into the main part of your template. (It would need to be done inside a <script> tag.)

(There are other options as well, but they get more and more “extreme” as you go along.)

Now I am using another html file and in main include it as u said :stuck_out_tongue: In my opinion it is a cheating hehe but it works :slight_smile:
Could you write exactly how should I change my code using json_script?

Thank you for your help!!

It’s described in the docs.

Give it a try, and if you still have issues, go ahead and post it here.

I made mistake with replace the syntax typical url adress:

url: '{% url "ajax_load_listanumerowproduktu" %}'

should be replace by:

url: '/APPLICATION_NAME/ajax/load_listanumerowproduktu/'

Maybe you did not check that because of not enough uploud code by me.
I create in main project directory urls.py and connect urls.py files from each application.
Thats why in my code is required ‘/APPLICATION_NAME/’ part at url Ajax…