所以我一直在寻找这个答案,而且我发现的大多数答案要么含糊不清,要么超出我对编程的了解.
我想要做的是在网页quad_relay.html上创建一个按钮
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o$ <html xmlns="http://www.w3.org/1999/xhtml"> </script> <head> <Meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <h1>Quad Web Relay</h1> <div>Relay 1: <input type="button" value="Toggle" id="toggle1" /> </div> </body> </head> </html>
单击该按钮时,我希望它运行python脚本set_gpio.py
def set_pins_high(): fp = open("/sys/class/gpio/gpio"+pin+"/value","w") fp.write(str(1)) fp.close() def set_pins_low(): fp1 = open("/sys/class/gpio/gpio"+pin+"/value","w") fp1.write(str(0)) fp1.close() for pin in pins: for x in range (0,2): set_pins_high() time.sleep(0.5) set_pins_low() time.sleep(0.5)
该网页位于运行Django的lighttpd服务器上.我安装了jQuery和AJAX,我知道它们是我需要采取的路线才能实现这一点,但每次我尝试它都不起作用.有人能帮助我吗?
谢谢!
解决方法
你必须做一些修改:
在您的HTML文件中:
<div>Relay 1: <form action= '{% url my_view_name%}' method="POST"> <input type="submit" value="Toggle" id="toggle1" /> </form> </div>
在urls.py中
url(r'whatever^$','core.views.my_view',name='my_view_name'),
在views.py中:
def my_view(request): if request.method == 'POST': import set_gpio #Do your stuff,calling whatever you want from set_gpio.py return #Something,normally a HTTPResponse,using django