如何在Django中从HTML标记按钮触发后台Python脚本?

问题描述 投票:1回答:2

请问,如何从我的Django平台运行python脚本?我有一个自定义的html页面,我有一个按钮标签(<button></button>),我想从中触发我在PDF上编写的python代码(精彩的解析器)。

实际上,如果这个应用程序的用户单击此解析它按钮,它会触发存储在我的静态文件夹中的python脚本,并在下表中打印提取的文本和图像:

我非常需要你的帮助和可能的想法。

谢谢!

python django python-3.x performance django-rest-framework
2个回答
1
投票

只需将脚本添加到视图中并像这样调用它。即在views.py文件中

<a href = {% url 'wonderful_script' %} class="btn btn-primary" role="button"> Wonderful Parser </a>

确保将该视图绑定到urls.py中的url。

url(r'^parse/$', wonderful_script, name='wonderful_script')

中间的wonderful_script是您在views.py中定义的视图。它必须包含解析器脚本的所有代码。


0
投票

如果您已经拥有要为其添加按钮的页面视图,则可以执行以下操作

{# test.html #}
<form action="" method="post">
    {% csrf_token %}
    <input type="submit" name="button_name" value="run_script">
</form>

和你的观点:

def test_view(requests):
    if(request.method == 'POST' && request.POST['button_name'] == 'run_script'):
        # do what you want when button clicked
    else:
        # other things in your views if you have
    return render(requests, 'test.html', {})
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.