1

我有一个使用 Ajax 每 10 秒获取数据的网页。我想改为每 60 秒调用一次,并让 Ajax 视图启动管理命令:“python manage.py collect_ticker -i”。这样,我可以在访问者打开页面时收集代码数据,而不是每 10 秒检查一次是否有任何新的代码数据。

我在 Ajax 视图中使用以下代码:

import subprocess
import os
import sys

fulldirname = os.path.abspath('../tickerproject/')


p = subprocess.Popen([fulldirname, 'python manage.py collect_ticker -i%d' % (i_ticker)], 
                                            stdout=subprocess.PIPE, 
                                            stderr=subprocess.STDOUT)

问题是我得到了回应:

[Errno 13] 权限被拒绝

问题:

1)在 Django 中允许站点访问者执行管理命令是否合适?

2)如果是,我如何在安全方面解决权限问题?

4

1 回答 1

2

Solution1: You can use call_command to run the command from django view without having permission issues:

from django.core.management import call_command
call_command('collect_ticker')

Solution2: you can directly call the function instead of management command from ajax/django view when visitor has the page open

ex: def page_view(request):
        management_command_replacement_function(params)
        return HttpResponse()

   def management_command_replacement_function(params)
       ####processing############
于 2014-04-29T07:41:04.670 回答