0

早上好,我正在尝试在 Otree 中使用 Google 验证码,我安装了 recaptcha 并创建了一个给我密钥的帐户,但是当我运行 powershell 时,它向我显示以下错误:


Traceback (most recent call last):
  File "c:\users\user\appdata\local\programs\python\python37\lib\site-packages\otree_startup\__init__.py", line 202, in do_django_setup
    django.setup()
  File "c:\users\user\appdata\local\programs\python\python37\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "c:\users\user\appdata\local\programs\python\python37\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "c:\users\user\appdata\local\programs\python\python37\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "c:\users\user\appdata\local\programs\python\python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\users\user\appdata\local\programs\python\python37\lib\site-packages\captcha\__init__.py", line 18, in <module>
    "Setting %s is not of type" % variable, instance_type
django.core.exceptions.ImproperlyConfigured: ('Setting RECAPTCHA_PUBLIC_KEY is not of type', <class 'str'>)

这是我设置中的代码,enviroment.get 代码用于在 Heroku 上运行实验。

RECAPTCHA_PUBLIC_KEY = ' RECAPTCHA_PUBLIC_KEY '

RECAPTCHA_PRIVATE_KEY = ' RECAPTCHA_PUBLIC_KEY '

RECAPTCHA_PUBLIC_KEY = environ.get('RECAPTCHA_PUBLIC_KEY')

RECAPTCHA_PRIVATE_KEY = environ.get('RECAPTCHA_PRIVATE_KEY')

关键是数字,我不知道如何使验证码有效,有人可以给出一个想法吗?

4

1 回答 1

1

我对该模块有同样的问题,我所做的是:

settings.py包装键中str()

RECAPTCHA_PUBLIC_KEY = str('YOUR_RECAPTCHA_PUBLIC_KEY')
RECAPTCHA_PRIVATE_KEY = str('YOUR_RECAPTCHA_PRIVATE_KEY')

我通过将调试打印直接插入到site-packages\captcha\__init__.py这样的中来检查它:

SETTINGS_TYPES = {
    "RECAPTCHA_DOMAIN": str,
    "RECAPTCHA_PRIVATE_KEY": str,
    "RECAPTCHA_PROXY": dict,
    "RECAPTCHA_PUBLIC_KEY": str,
    "RECAPTCHA_VERIFY_REQUEST_TIMEOUT": int,
}

print(settings.RECAPTCHA_PRIVATE_KEY) # <--added
print(type(settings.RECAPTCHA_PRIVATE_KEY)) # <--added

# Validate settings types.
for variable, instance_type in SETTINGS_TYPES.items():
    if hasattr(settings, variable) \
            and not isinstance(getattr(settings, variable), instance_type):
        raise ImproperlyConfigured(
            "Setting %s is not of type" % variable, instance_type
        )

然后我输入python manage.py shell查看输出。实际上,RECAPTCHA_PRIVATE_KEY 的类型unicode不是str.

于 2020-11-19T22:35:56.640 回答