简体   繁体   中英

call Django view function from django HTML template

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM