简体   繁体   English

从 Django 中的视图执行 function

[英]Execute function from view in Django

i need help with my django project.我的 django 项目需要帮助。 I am trying to open folder from django view and I came up with idea which will execute python script which will handle this part, because for some security reasons Chrome is not able to directly open file explorer (program).我正在尝试从 django 视图打开文件夹,我想出了一个想法,它将执行 python 脚本来处理这部分,因为出于某些安全原因,Chrome 无法直接打开文件资源管理器(程序)。

So I tried this.所以我尝试了这个。

detail.html looks: detail.html 看起来:

<form class="form" method='post'>
  {% csrf_token %}
  <button style="width:100%;" type='explorer' name='explorer' value='explorer' class="btn btn-info btn-min-width"><i class="fa fa-folder"></i> Navigate to folder</button>
</form>

actions.py looks: actions.py 看起来:

def openexplorer():
    return os.system('explorer')

view.py looks: view.py 看起来:

def detail(request, Pools_id):
    try:
        je = More.objects.get(pk=Pools_id)
    except More.DoesNotExist:
        raise Http404("Question does not exist")
    return render(request, 'rc/detail.html', {'more_identification': je})

    if request.method == 'POST':
        if 'explorer' in request.POST:
            openexplorer()

I am rendring the page and i only want to execute the script when the button is clicked.我正在渲染页面,我只想在单击按钮时执行脚本。 Can someone please help?有人可以帮忙吗? I think there is some error in view.py.我认为 view.py 中有一些错误。

Thanks a lot !非常感谢 !

Okay, I found it.好的,我找到了。 just put the if statement under try and it works fine:) That's cool只需将 if 语句放在 try 下,它就可以正常工作:) 这很酷

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

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