Python Django官方教程,测试客户端,上下文
我正在尝试做“传奇”django教程,但遇到了一些问题。首先,我正在通过交互式shell开发Django测试客户机。我遇到的具体问题与请求有关:Python Django官方教程,测试客户端,上下文,python,django,Python,Django,我正在尝试做“传奇”django教程,但遇到了一些问题。首先,我正在通过交互式shell开发Django测试客户机。我遇到的具体问题与请求有关: >>> response.context['latest_question_list'] 我得到的回应是 Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: 'NoneType'
>>> response.context['latest_question_list']
我得到的回应是
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'
如果这个问题写得不好,我道歉,因为这是我第一次问。此外,对于那些更好奇或需要关于我的代码的更多信息的人,请访问此git的链接,其中包含一个最新示例-
非常感谢任何有足够耐心帮助我解决这个问题的人
------------->>弹药
基于jpic的回答,希望这能解释我是如何得到响应对象的:
>>> from django.test import Client
>>> # create an instance of the client for our use
>>> client = Client()
>>> # get a response from '/'
>>> response = client.get('/')
>>> from django.core.urlresolvers import reverse
>>> response = client.get(reverse('polls:index'))
>>> from polls.models import Question
>>> from django.utils import timezone
>>> response = client.get('/polls/')
>>> response.content
>>> response.context['latest_question_list']
我希望这能回答你的问题。非常感谢您尽快回来!再次为格式化道歉 欢迎
您的问题的问题是,您没有向我们展示如何获得与您自己相同的响应对象,因此,我们无法重现您的问题
您的上下文对象的问题是它是None,request.context在您的情况下是None,所以这就是为什么“[…]”调用\uuuu getitem\uuuuuu
-失败的原因,因此出现异常“NoneType没有属性\uu getitem\uuuuu
”
对于我来说,django测试客户机的响应具有正确的上下文:
$ ./manage.py test polls
Creating test database for alias 'default'...
.WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
--Return--
None
> /tmp/04_django_intro/mysite/polls/tests.py(43)test_index()
41 def test_index(self):
42 response = self.client.get('/')
---> 43 import ipdb; ipdb.set_trace()
ipdb> response.context
[{'False': False, 'None': None, 'True': True}, {'exception': 'Resolver404', 'request_path': u'/'}]
ipdb> c
...
----------------------------------------------------------------------
Ran 4 tests in 5.121s
OK
Destroying test database for alias 'default'...
要在我的测试中获取此调试器,以下是我添加到polls/test.py的代码:
def test_index(self):
response = self.client.get('/')
import ipdb; ipdb.set_trace()
不要忘了pip安装ipdb
,或者在此处使用pdb
而不是ipdb
也请考虑阅读,如果你还没有,但也许你在开始投票/测试之后读过。P../P>< P>如果使用JICAN2模板而不是DTL,OP所描述的问题也会发生。 因此,如果您按照教程中的说明进行操作,但修改了
settings.py
以使用jinja2,那么python shell中将包含以下行:
import django
django.setup()
from django.test.utils import setup_test_environment
setup_test_environment()
from django.test import Client
# create an instance of the client for our use
client = Client()
# get a response from '/'
response = client.get('/')
==> Not Found: /
response.status_code
==> 404
from django.urls import reverse
response = client.get(reverse('polls:index'))
response.status_code
==> 200
response.context['latest_question_list']
==> Traceback (most recent call last):
Python Shell, prompt 12, line 1
builtins.TypeError: 'NoneType' object is not subscriptable
正如您所看到的,response.context
是None,因为Jinja2没有填充它。使用Jinja2时,请改用context\u data
:
response.context_data['latest_question_list']
==> <QuerySet [<Question: What's up?>]>
response.context\u数据['latest\u question\u list']
==>response.context为None,如何获取响应对象?嘿,jpic?这是否回答了您的问题?response.content
显示了什么?您是否记得设置测试环境,如图所示?
response.context_data['latest_question_list']
==> <QuerySet [<Question: What's up?>]>