从Django运行python脚本 [英] Running python script in Django from submit
问题描述
可能有一种不同的方法来解决这个问题,但我对使用Django来说相当新鲜。
Perhaps there is a different way of going about this problem, but I am fairly new to using Django.
我已经编写了一个自定义的python脚本,并希望当用户按下网页上的提交按钮时,运行一个函数或.py文件。
I have written a custom python script and would like to run a function or .py file when a user presses a "submit" button on the webpage.
如何从一个参数传递给一个python函数提交按钮使用Django?
How can I get a parameter to be passed into a python function from a submit button using Django?
推荐答案
通常情况下,您的表单将提交一个帖子请求。然后,您将拦截您的urls.py中的请求,您可以在其中调用该函数。所以如果你的表单看起来像这样:
Typically what is done is you'd have your form submit a post request. You'd then intercept the request in your urls.py, where you'd call your function. So if your form looks like this:
<form action="submit" method="post">
<input type="text" name="info"><br>
<input type="submit" value="Submit">
</form>
你的urls.py会有这样的东西:
your urls.py would have something like this:
url(r'^submit', views.submit)
,您的views.py将具有获取通过帖子传递的参数的功能:
and your views.py would have the function that would get the parameters that were passed through the post:
def submit(request):
info=request.POST['info']
# do something with info
此链接给出了更深入的解释。
This link gives a more in depth explanation.
这篇关于从Django运行python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!