1

I have a button to copy data from a user uploaded file to the clipboard in a specific format. I already have that data saved in the database as it was uploaded in a separate file form. I currently have it so that upon clicking the copy to clipboard button it is linked to a copy_data view in my views.py that requires an HTTP request which redirects to the current template containing the copy to clipboard button with something like this:

HttpResponseRedirect('previous/template/here')

This works fine except for the fact that since it links to my copy_data view which then redirects to the original view containing the button it reloads the entire page which is undesirable.

I think a better solution would be to somehow bind a python function directly to the button click rather than worrying about redirecting from one view to another.

I've found many examples using ajax, but haven't found any that work for my use case. I tried binding a click event to the button without any problems, but I am stuck on figuring out how to bind the python function with the click.

How can I bind a python function in my Django template upon a button press?

alacy
  • 4,462
  • 5
  • 25
  • 45
  • I am not 100% sure what you're asking. The browser will not execute arbitrary python code - you can't put python into the template as you would javascript. You can send data asynchronously to the server where it can then run through python code, but that's an Ajax call and you said that's not what you're looking for. However, I can't think of a way to accomplish this without a refresh / full page load without using Ajax. – souldeux Jan 17 '15 at 02:01
  • But if all I want is to execute the python script that copies the data that is already present in the database to clipboard what data would I send via Ajax? I guess I could send a flag upon a click event that the Django view interprets as a go for the copying to clipboard. – alacy Jan 17 '15 at 02:25
  • Your server-side python code is not going to be able to write to an client's clipboard. It can fetch data from the database, process it, and then send that processed data to the client. Once the data has been passed from the server to the client, you can use javascript on the client side to access the user's clipboard (there are security concerns with this, see here: http://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript). A locally run python script could access the clipboard directly, but it's not going to work like that through a server/client architecture. – souldeux Jan 17 '15 at 05:04

1 Answers1

1

It's tough to tell for sure, but I think you're mixing synch/asynch paradigms here. When you generate requests with Ajax, you don't (generally) want to return a redirect, you want to return data. That might be JSON data or data formatted as a specific MIME type or even just text. One way this might look at a high level is:

def copy_data(request):
    # get posted data
    submitted = request.POST
    # do whatever is necessary to create document
    data = ???
    # first, we'll need a response
    resp = HttpResponse() 
    # set the content type, if needed
    resp.content_type = 'text/???; charset=utf-8'
    # response has a file-like interface
    resp.write(data)
    return resp

Obviously, this would need work to suit your purpose, but that's the high-level approach.

It doesn't sound like you're returning JSON, but there's a special response object for that now if you need it.

dylrei
  • 1,564
  • 9
  • 13
  • After thinking more about it I realized my question was way off my aim so I updated it. Thanks for your answer though. – alacy Jan 17 '15 at 00:30
  • @aus_lacy I think you might not fully understand the separation of concerns between the server and the browser. Ajax doesn't know or care that it's talking to Django and vice versa. The trick is to get them cooperating by using common request/response patterns. My answer might not be exactly what you need, but a working solution is going to look a lot more like my answer than the path you seem to be heading down. – dylrei Jan 17 '15 at 03:03