Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Django远程服务中调用management.call_命令?_Python_Django_Ldap - Fatal编程技术网

Python 如何在Django远程服务中调用management.call_命令?

Python 如何在Django远程服务中调用management.call_命令?,python,django,ldap,Python,Django,Ldap,我有一个Python作业脚本,它调用命令来更新广告用户。当我在本地机器上运行此命令时,它工作正常。但是在我们的远程服务器上,使用Linux和Apache时,相同的脚本不起作用,并给了我一个错误:“未知命令:'ldap_sync_users'…我尝试使用其他常见命令,如migrate(只是为了测试),它给出了相同的问题 这是我正在尝试运行的命令,在我的本地系统中运行良好: # https://github.com/etianen/django-python3-ldap management.

我有一个Python作业脚本,它调用命令来更新广告用户。当我在本地机器上运行此命令时,它工作正常。但是在我们的远程服务器上,使用Linux和Apache时,相同的脚本不起作用,并给了我一个错误:“未知命令:'ldap_sync_users'…我尝试使用其他常见命令,如migrate(只是为了测试),它给出了相同的问题

这是我正在尝试运行的命令,在我的本地系统中运行良好:

# https://github.com/etianen/django-python3-ldap   
management.call_command('ldap_sync_users')
我使用的是Python 3.6,Django 2.0.4。在localserver中,我使用的是virtualenv,在远程服务器Apache 2中,我做错了什么?有人能帮我吗

view.py

...
from apps.iumob.utilities import sync_hr_with_ldap

def cron_job_to_sync(request):
    print(request.GET.get('token'))
    if request.GET.get('token') == "mytoken":
        if sync_hr_with_ldap() is True:
            return HttpResponse("Records synchronization finished.")
        else:
            return HttpResponse("Records synchronization finished with errors.")
    else:
        return HttpResponse("Access forbidden.")
utilities.py:

from django.core import management

def sync_hr_with_ldap():
"""
This method will be called to synchronize AD users and workers.
If some worker that have a user and he was fired, it will check all users and, if it is still active,
it will save in database this record. Every time that the user is deactivate from AD,
this record will also be updated.
"""

# This method call a library that call LDAP and check all users, inactivating if necessary
# https://github.com/etianen/django-python3-ldap
management.call_command('ldap_sync_users')`
回溯

Environment:


Request Method: GET
Request URL: myurl.../pt-br/iumob/cron-job-sync?token=sometoken

Django Version: 2.0.3
Python Version: 3.5.2
Installed Applications:
['apps.iumob.apps.IumobConfig',
 'apps.hrmob.apps.HrmobConfig',
 'apps.cimob.apps.CimobConfig',
 'apps.users.apps.UsersConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_python3_ldap',
 'rest_framework',
 'rest_framework.authtoken']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py" in call_command
  102.             app_name = get_commands()[command_name]

During handling of the above exception ('ldap_sync_users'), another exception occurred:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
  35.             response = get_response(request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/var/www/marcomob/apps/iumob/views.py" in cron_job_to_sync
  43.         if sync_hr_with_ldap() is True:

File "/var/www/marcomob/apps/iumob/utilities.py" in sync_hr_with_ldap
  19.     management.call_command('ldap_sync_users')

File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py" in call_command
  104.             raise CommandError("Unknown command: %r" % command_name)

Exception Type: CommandError at /pt-br/iumob/cron-job-sync
Exception Value: Unknown command: 'ldap_sync_users'

详细信息:当我尝试在远程服务中通过ssh执行相同的命令时,一切正常……只是python脚本中没有。python manage.py ldap\u sync\u users请显示一个复制问题的最小脚本。听起来好像
django-python3-ldap
不在
已安装的应用程序中,当它失败时-也许您没有配置设置正确。感谢Alasdair。但是模块已安装在应用程序中。我可以使用LDAP登录,正如我之前所评论的,当我尝试直接在promt上执行python manage.py LDAP\u sync\u用户时(即使在远程服务器中),它工作得很好。只是python脚本没有识别这个命令,只是在远程服务器中。我将在问题中添加完整的脚本。我仍然不清楚发生了什么。如果您是从视图调用它,那么显示视图。完整的回溯可能也会有所帮助。添加了回溯。