从提交在 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.
如何使用 Django 从提交按钮获取要传递给 Python 函数的参数?
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屋!