0

I am new to Django but i am advanced programmer in other frameworks.

What i intend to do:

Press a form button, triggering Javascript that fires a Ajax request which is processed by a Django View (creates a file) that return plain simple JSON data (the name of the file) - and that is appended as a link to a DOM-Element named 'downloads'.

What i achieved so far instead:

Press the button, triggering js that fires a ajax request which is process by a Django view (creates a file) that return the whole page appended as a duplicate to the DOM-Element named 'downloads' (instead of simple JSON data).

here is the extracted code from the corresponding Django view:

context = {
    'filename': filename
}
data['filename'] = render_to_string(current_app+'/json_download_link.html', context)
return HttpResponse(json.dumps(data), content_type="application/json")

I tried several variants (like https://stackoverflow.com/a/2428119/850547), with and without RequestContext object; different rendering strats.. i am out of ideas now..

It seems to me that there is NO possibility to make ajax requests without using a template in the response.. :-/ (but i hope i am wrong) But even then: Why is Django return the main template (with full DOM) that i have NOT passed to the context... I just want JSON data - not more!

I hope my problem is understandable... if you need more informations let me know and i will add them.

EDIT:

for the upcoming questions - json_download_link.html looks like this:

<a href="{% url 'foobar:download' customer filename %}">Download</a>

But i don't even want to use that!

corresponding jquery:

    $.post(url, form_data)
    .done(function(result){
        $('#downloads').append('<a href="'+baseUrl+'/download/'+result['filename']+'" title="download"> Download CSV</a>')
    })
Community
  • 1
  • 1
tector
  • 977
  • 1
  • 8
  • 31

2 Answers2

0

I don't understand your question. Of course you can make an Ajax request without using a template. If you don't want to use a template, don't use a template. If you just want to return JSON, then do that.

Without having any details of what's going wrong, I would imagine that your Ajax request is not hitting the view you think it is, but is going to the original full-page view. Try adding some logging in the view to see what's going on.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • the correct view is processed, i checked it via test outputs. when i do sth. like: return HttpResponse(json.dumps({'foo':'bar'}), content_type="application/json") the result is the same: the full DOM will be duplicated and appended where only the filename link should be appended.. – tector Dec 19 '13 at 15:27
  • Sorry, that is not possible. It is not coming from the view you have above, that's all I can say. Django would not magically recreate your whole page without being told to. – Daniel Roseman Dec 19 '13 at 16:01
0

There is no need to return the full template. You can return parts of template and render/append them at the frontend.

A template can be as small as you want. For example this is a template:

name.html
<p>My name is {{name}}</p>

You can return only this template with json.dumps() and append it on the front end.

What is your json_download_link.html?

assuming example.csv is string

data ={}
data['filename'] = u'example.csv'
return HttpResponse(simplejson.dumps(data), content_type="application/json")

Is this what you are looking for?

user3030969
  • 1,505
  • 2
  • 15
  • 17