문제

I want to learn more details about HttpResponse inside django.http so I write the test code on shell as i see on the django book.

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

but it return an error that says:

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.

so i guess if there's any jobs must to be done before test on shell, am i right? Configure settings or something ? where ?

도움이 되었습니까?

해결책

You need test the code in a Django Project.

Try test that code in a Django Shell into a project.

Example:

$ ./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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top