Django教程错误:设置测试客户端

时间:2015-03-30 08:03:18

标签: python django python-2.7 pycharm django-testing

我在Django教程的第5部分,我遇到了障碍。我试图在python shell中使用以下命令设置测试客户端:

from django.test.utils import setup_test_environment
setup_test_environment()

我收到以下错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mohitgupta/.virtualenvs/Django/lib/python2.7/site-packages/django/test/utils.py", line 104, in setup_test_environment
    mail._original_email_backend = settings.EMAIL_BACKEND
  File "/Users/mohitgupta/.virtualenvs/Django/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
    self._setup(name)
  File "/Users/mohitgupta/.virtualenvs/Django/lib/python2.7/site-packages/django/conf/__init__.py", line 40, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting EMAIL_BACKEND, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

我已经研究过定义DJANGO_SETTINGS_MODULE而没有真正的进展。请注意,我使用PyCharm作为IDE,但我不使用他们提供的内部终端。

有什么建议吗?

2 个答案:

答案 0 :(得分:4)

如本教程前面部分所述,要在Django中启动shell,您应该始终执行./manage.py shell,而不是直接启动Python。这为你创造了环境。

答案 1 :(得分:1)

我有一个类似的问题,在我在shell中发出命令之后它才起作用

import django
django.setup()