Django测试视图

2024-06-16 11:26:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试django项目中的一个视图。我无法从那种观点中得到背景。 我在贝壳里工作。I类型:

>>> from django.test.client import Client
>>> c = Client()
>>> c.login(username='Test', password='test')
True
>>> response = c.get(reverse('lista_categorie'))
>>> response.status_code
200
>>> response.context['categorie']
Traceback (most recent call last):
 File "<console>", line 1, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'

我有一个变量'category'作为上下文提供给模板,所以我无法理解为什么上下文是空的。状态代码是ok,并且内容也响应模板。 以下是视图:

def lista_categorie(request):
   categorie = Categoria.objects.all()
   return render_to_response('view_categories.html', {
    'categorie': categorie,
}, context_instance=RequestContext(request))   

有什么想法吗?你知道吗


Tags: 项目djangofromtestclient视图模板类型