我正在测试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))
有什么想法吗?你知道吗
先前的答案是错误的。以下是来自http://jazstudios.blogspot.com/2011/01/django-testing-views.html的解决方案,添加此行以便可以在测试期间访问上下文:
我没有亲自测试过,你可以测试一下。你知道吗
相关问题 更多 >
编程相关推荐