세션이 작동하지 않는 것 같습니다. Django는 HttpRequest 객체가 'session'이라는 속성을 가지고 있지 않다고 불평합니다. 이 문서에는 미들웨어가 활성화되어 있고 설치된 응용 프로그램에 django.contrib.sessions가 있으면 잘 할 수 있다고 명시되어 있습니다. 단위 테스트를 사용하여이 오류가 발생합니다. 내 views.py에서django - HttpRequest 객체에 'session'속성이 없습니다.
:
def home_page(request):
response = render(request, 'home.html', {'message_text' : request.session.get('message_text', ''),
'ip_address' : request.session.get('ip_address', ''),
'port_number' : request.session.get('port_number', ''),
'command_text' : request.session.get('command_text', ''),})
request.session['message_text'] = ''
return response
내가 얻으려고 세션 값은 내가 다른 곳 views.py에서 내 양식 포스트 방법을 설정하기 위해 노력하고있어 그들이다.
또한 새 프로젝트에서 기본적으로 사용하도록 설정되어 있습니다. 그래서 새로운 새로운 django 프로젝트를 만들고 콘솔의 세션 속성을 확인했습니다. 정확히 내가 한 일이 여기에 있습니다.
(django1.5)Python $ django-admin.py startproject testing
(django1.5)Python $ cd testing/
(django1.5)testing $ python manage.py shell
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.http import HttpRequest
>>> r = HttpRequest()
>>> r.session
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'HttpRequest' object has no attribute 'session'
>>>
무엇이 부족합니까 ??
업데이트 : 이것은 단위 테스트로 테스트 할 때만 발생합니다. 여기에 예외가 발생 시험은 다음과 같습니다
def test_home_page_returns_correct_html(self):
request = HttpRequest()
response = home_page(request)
expected_html = render_to_string('home.html')
self.assertEqual(response.content, expected_html)
'django.utils.importlib' 장고 1.7 이후 사용되지 않는 것으로 표시되고 장고 1.9에서 제거되었다. Django> = 1.9 인 경우'from importlib import import_module'을 대신 사용하십시오. –