0

like same problem but not solve--> Send Dynamic AJAX URL in Django

i want to data extraction by using dynamically url.

urls.py

url(r'^(?P<idx>\d+)/rapor/$',ReportView.as_view(),name='rapor'),
url(r'^(?P<idx>\d+)/rapor/hamveri/$',ChartData.as_view(),name='hamveri'),

report.html

var endpoint ='/analiz/{{ data.id }}/rapor/hamveri/';
var defaultData = [];
var labels = [];


$.ajax({
method: "GET",
url: endpoint,
success: function(data){
     labels = data.labels;
     defaultData = data.default;
     setChart()
},
error: function(error_data){
     console.log("error");
     console.log(error_data)
 }});

well-done data send to web address but i can't data extraction from this url

http://127.0.0.1:8000/analiz/33/rapor/

how can i fixed this problem?

if i edit report.html

report.html

var endpoint ='/analiz/33/rapor/hamveri/';

perfect-run web site, it can data extraction from 'analiz/33/rapor/hamveri/'

and so; http://127.0.0.1:8000/analiz/33/rapor/

but not dynamically url :(

Thanks in advance for your help.

1
  • If you inspect element over your HTML, what do you see in place of " var endpoint ='/analiz/{{ data.id }}/rapor/hamveri/'; " ? Do you see the tag " {{ data.id }} " or the value sent by you through your views? Commented Aug 9, 2017 at 4:26

2 Answers 2

2

What you can try is:

Write this HTML just before the JavaScript starts. Then change the variable:

var endpoint ='/analiz/{{ data.id }}/rapor/hamveri/'; as described below,

Somewhat like this:

<div id="endpoint-url-div" url="{% url 'hamveri' data.id %}" style="display: none;"></div>
<script type="text/javascript">
    var endpoint = document.getElementById('endpoint-url-div').getAttribute('url');
    //Now endpoint should have a URL like **/analiz/33/rapor/hamveri/**
    //Add the rest of your JavaScript as it is. 
    var defaultData = [];
    var labels = [];

     $.ajax({
         method: "GET",
         url: endpoint,
         success: function(data){
                  labels = data.labels;
                  defaultData = data.default;
                  setChart()
              },
         error: function(error_data){
                   console.log("error");
                   console.log(error_data)
             },
        });
    </script>

Make sure that variable is sent correctly from your views. Make sure you have a variable inside your context dictionary inside the views.

views.py must have something like:

context = dict()
context['data'] = #Some data model object

EDIT:

As I can see in your analiz/views.py, you are not sending any idx or model data object to your repor.html.

Not sending idx or model data object to repor.html will not give you a url like: /analiz/33/rapor/hamveri/ inside the variable endpoint.

In your views, you have to send a model object

class ReportView(View):
def get(self, request, *args, **kwargs):
    data = UploadFile.objects.get(...) #Get the model object here. Put something like:  user = request.user
    context = {'data' : data}
    return render(request, 'analiz/rapor.html', context)

Now in your HTML:

<div id="endpoint-url-div" url="{% url 'hamveri' data.id %}" style="display: none;"></div>
<script type="text/javascript">
    var endpoint = document.getElementById('endpoint-url-div').getAttribute('url');
    ...
    ...
    ... #Rest of the Javascript# ...
    ...
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

@didierdrogba I made an edit. Check if anything is printed in your browser console.
bro thanks for help but i cant. if you download file, maybe you can dosya.tc/server9/dwbias/istatistik.rar.html but you didn't again thanks.
@didierdrogba I read you code and see the problem. I've made an EDIT. Please refer to it and see if it works.
but i firstly sending to "analiz/idx/rapor/hamveri" by class ChartData(APIView): then using ajax data extraction from this url. not necessary object.get() function for Reportview, so this class just directly to html page (raport.html) and in raport.html, i can data extraction using ajax jquery. isnt it?
i think, just editable code ajax method but i dont known how to edit.
|
0

Yes, well-done

i make mistake in a critical point.

@shivamsharma said that you are not sending any idx or model data object to your repor.html.

So this function:

class ReportView(View):
def get(self, request,idx,*args, **kwargs):
    return render(request, 'analiz/rapor.html', {'dataninidx':idx}) # added new key

Then i change variable in report.html

var endpoint='/analiz/{{ dataninidx }}/rapor/hamveri/';

Yes, the program only works with two changes.

Thanks for your help, @shivam-sharma

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.