请问,如何从我的Django平台运行python脚本?我有一个自定义的html页面,我有一个按钮标签(<button></button>
),我想从中触发我在PDF上编写的python代码(精彩的解析器)。
实际上,如果这个应用程序的用户单击此解析它按钮,它会触发存储在我的静态文件夹中的python脚本,并在下表中打印提取的文本和图像:
我非常需要你的帮助和可能的想法。
谢谢!
只需将脚本添加到视图中并像这样调用它。即在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中定义的视图。它必须包含解析器脚本的所有代码。
如果您已经拥有要为其添加按钮的页面视图,则可以执行以下操作
{# 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', {})