-1

I have a simple upload function in Python which I have put in my views.py.

def upload():

    global original_img,img,img2,img3,image_path,old_label_image,photo,label,image_path,image,ax,fig
    print "upload"
    image_path=tkFileDialog.askopenfilename()
    image = Image.open(image_path)
    original_img= image.copy()
    image.thumbnail((1000,625))
    photo = ImageTk.PhotoImage(image)
    label = Label(image=photo)
    label.image = photo
    if old_label_image is not None:
        old_label_image.destroy()
        old_label_image = label
    label.pack()

I want this function to be called onclick of a button from an html template file(current.html) in Django.

 <button> Upload from PC</button>

How do I call the view function on click of the button in HTML? Also do I have to make any changes in URLConfs for this? Please help

ruddra
  • 50,746
  • 7
  • 78
  • 101
naiveDeveloper
  • 625
  • 2
  • 11
  • 28
  • Do it with javascript, the only way. – Henrik Andersson Jun 27 '14 at 05:19
  • I am just two days old in Django. Can you please tell me where and how I should out the javascript code ? and whatis the role of the javascript code? – naiveDeveloper Jun 27 '14 at 06:03
  • @limelights is completely wrong here. Not only is JavaScript *not* "the only way", it wouldn't even work at all since you want to submit a file upload form. – Daniel Roseman Jun 27 '14 at 06:57
  • But as I said in my answer to your previous question which you ignored, you can't simply convert desktop code to web code like that. – Daniel Roseman Jun 27 '14 at 06:59
  • Ya..I agree.I was looking for ways in which I can convert my desktop code to a web app. I am in deep ocean here since there is too much information on the net and I cant zero in on anything. Please help me with the possible ways. – naiveDeveloper Jun 27 '14 at 07:04
  • @DanielRoseman Not completely wrong (https://blueimp.github.io/jQuery-File-Upload/) and further more, I believe, I was wrong-ish since I read that he wants to do it with the `onclick` on a button. – Henrik Andersson Jun 27 '14 at 07:07
  • @adrita please go and do the Django tutorial, then look at the docs on forms. Then come back with any *specific* questions. – Daniel Roseman Jun 27 '14 at 07:19

2 Answers2

1

In html, you can use JavaScript's onlick this way:

<input type="button" onclick="window.location.href='/upload/'"> </input> 

Note: "/upload/" will redirects to whichever view "/upload/" is mapped to in urls.py

EDIT:

Please find the reference below HTML button onclick event

Community
  • 1
  • 1
Nike
  • 72
  • 6
1

In html

You can add this code to your button

<a href="/appname/upload/"><button >Upload from PC</button></a>

This works for me fine

John
  • 666
  • 1
  • 9
  • 22