简体   繁体   English

如何使用 Django 在 HTML 按钮按下时执行 file.py?

[英]How to execute file.py on HTML button press using Django?

My goal is to click an HTML button on my Django web page and this will execute a local python script.我的目标是单击我的 Django 网页上的 HTML 按钮,这将执行本地 python 脚本。

I am creating a local web application as an interface to a project.我正在创建一个本地 Web 应用程序作为项目的接口。 This will not be hosted and will always just run on my local machine.这不会被托管,只会在我的本地机器上运行。 My project is run with a python script (which carries out numerous tests specific to my project).我的项目使用 python 脚本运行(它执行针对我的项目的大量测试)。 All I need is for a button in my web interface to execute this script on my machine.我所需要的只是在我的 Web 界面中有一个按钮来在我的机器上执行这个脚本。

I have a template index.html where the whole web page is located.我有一个模板 index.html 整个网页所在的位置。 I presume I need to call some form of views function possibly when the button is pressed?我想我可能需要在按下按钮时调用某种形式的视图函数?

How to execute python code by django html button? 如何通过 django html 按钮执行 python 代码?

This question suggests:这个问题暗示:

def index(request):
    if request.method == 'GET':
        return render(request, 'yourapp/index.html', {'output': ''})
    elif request.method == 'POST':
        py_obj = mycode.test_code(10)
        return render(request, 'yourapp/output.html', {'output': py_obj.a})

I tried this just as a test but nothing happened when I went to the URL (located in the appropriate views.py):我试过这个只是作为测试,但是当我转到 URL(位于相应的 views.py 中)时什么也没发生:

def runtest(request):
    print("Hello World")
    Popen(['gnome-terminal', '-e', 'echo "Hello World"'], stdout=PIPE)
    return

However I don't quite understand if this achieves what I need it to, I am struggling to understand what the answer is suggesting.但是,我不太明白这是否达到了我的需要,我正在努力理解答案所暗示的内容。

Where in the Django framework can I specify a call to a local python script when a button is pressed?当按下按钮时,我可以在 Django 框架中的哪个位置指定对本地 python 脚本的调用?

(I have very limited experience with web applications, this is simply just meant to be a simple interface with some buttons to run tests) (我对 Web 应用程序的经验非常有限,这只是一个简单的界面,带有一些用于运行测试的按钮)

what you're going to want to try and do is submit a form on the button click.您将要尝试做的是在单击按钮时提交表单。 You can then import the functions you want to run from the script and call them in your view.然后,您可以从脚本中导入要运行的函数并在您的视图中调用它们。 You then redirect to the same page.然后您重定向到同一页面。

I hope this helps!我希望这有帮助!

index.html索引.html

<form method="post">
    {% csrf_token %}
    <button type="submit" name="run_script">Run script</button>
</form>

views.py视图.py

if request.method == 'POST' and 'run_script' in request.POST:

    # import function to run
    from path_to_script import function_to_run

    # call function
    function_to_run() 

    # return user to required page
    return HttpResponseRedirect(reverse(app_name:view_name)

Adding to answer above.添加到上面的答案。 You can run the function in a different view completely:您可以完全在不同的视图中运行该函数:

<form method="post" action="{% url 'app:view/function' %}"> {% csrf_token %} <button class="btn btn-danger btn-block btn-round">Perform task</button> </form>

And render whatever template you want (same template will execute task but seem like nothing has happened).并渲染您想要的任何模板(相同的模板将执行任务,但似乎什么也没发生)。 This is handy if you already have a 'POST' form handler.如果您已经有一个“POST”表单处理程序,这很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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