在python shell上测试django.http但是失败了

时间:2014-05-11 03:56:00

标签: python django shell

我想在django.http中了解有关HttpResponse的更多详细信息 所以我在django书上看到了在shell上编写测试代码。

    >>> from django.http import HttpResponse
    >>> response = HttpResponse("Here’s the text of the Web page.")
    >>> response = HttpResponse("Text only, please.", content_type="text/plain")

但它返回一个错误,上面写着:

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    response = HttpResponse("Here's the text of the Web page.")
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/http/response.py", line 327, in __init__
    super(HttpResponse, self).__init__(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/http/response.py", line 103, in __init__
    self._charset = settings.DEFAULT_CHARSET
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/__init__.py", line 54, in __getattr__
    self._setup(name)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/conf/__init__.py", line 47, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting DEFAULT_CHARSET, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

所以我想如果在测试shell之前必须完成任何工作,我是对的吗? 配置设置或什么?在哪里?

1 个答案:

答案 0 :(得分:2)

您需要在Django项目中测试代码。

尝试将Django Shell中的代码测试到项目中。

示例:

$ ./manage.py shell

>>> from django.http import HttpResponse
>>> response = HttpResponse("Here's the text of the Web page.")
>>> response
<django.http.response.HttpResponse object at 0x4182210>