Django:如何搜索 django.template.context.RequestContext
Django: How to search through django.template.context.RequestContext
我正在 Django 中进行测试并面临 ,我正在尝试遍历并找到 里面的物体。
test.py
def test_ProductDetail_object_in_context(self):
response = self.client.get(reverse('product_detail', args=[1]))
# assertEqual - test passes
self.assertEqual(response.context[0]['object'], Product.objects.get(id=1))
# assertIn - test fails
self.assertIn(Product.objects.get(id=1), response.context[0])
views.py
class ProductDetailView(DetailView):
model = Product
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
data = cartData(self.request)
cartItems = data['cartItems']
context['cartItems'] = cartItems
return context
里面有什么response.context:
[
[
{'True': True, 'False': False, 'None': None},
{'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0x7fd80>>,
'request': <WSGIRequest: GET '/1/'>,
'user': <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7fd820>>, '
perms': <django.contrib.auth.context_processors.PermWrapper object at 0x7fd80>,
'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x7fd8290>,
'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}
},
{},
{'object': <Product: Pen>,
'product': <Product: Pen>,
'view': <ecom.views.ProductDetailView object at 0x7fd8210>,
'cartItems': 0}
],
[
{'True': True, 'False': False, 'None': None},
{'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0x7fd8240>>,
'request': <WSGIRequest: GET '/1/'>,
'user': <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7fd8250>>,
'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x7fd8250>,
'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x7fd8290>,
'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}
},
{},
{'object': <Product: Pen>,
'product': <Product: Pen>,
'view': <ecom.views.ProductDetailView object at 0x7fd8210>,
'cartItems': 0}
]
]
response.context 的类型:
<class 'django.template.context.RequestContext'>
Product.objects.get(id=1)里面的内容是:Pen
Product.objects.get(id=1) 的类型是:<class 'ecom.models.Product'>
我不明白为什么:
它在 self.assertEqual(response.context[0]['object'], Product.objects.get(id=1)) 中找到了 Product 对象,但是不在 self.assertIn(Product.objects.get(id=1), response.context[0]['object']) - 说 TypeError: argument of type 'Product' is not iterable
它也没有在 self.assertIn(Product.objects.get(id=1), response.context[0]) 中找到它 - 说“AssertionError: <产品:Pen> 在 [....response.context[0]...] 中找不到
的内容
它也没有在 self.assertIn(Product.objects.get(id=1), response.context[0][3]) 中找到它 - 说“在 getitem 引发 KeyError(key), KeyError: 3"
如何使用 RequestContext class? JSON喜欢?
抱歉,我的问题有点混乱,只是想了解如何使用 RequestContext。
提前致谢!
我认为您的测试失败了,因为 assertIn
通过 KEYS 而不是值来查看。解决方案是:
self.assertIn(Product.objects.get(id=1), response.context[0].values())
多一点解释:response.context[0]
似乎是一些键值存储,即字典。当您执行 response.context[0]["object"]
时,您刚刚访问了键“对象”处的值,其中 response.context[0]
是字典。在字典上做一些 in
查询只会查找字典的键。
我正在 Django 中进行测试并面临
test.py
def test_ProductDetail_object_in_context(self):
response = self.client.get(reverse('product_detail', args=[1]))
# assertEqual - test passes
self.assertEqual(response.context[0]['object'], Product.objects.get(id=1))
# assertIn - test fails
self.assertIn(Product.objects.get(id=1), response.context[0])
views.py
class ProductDetailView(DetailView):
model = Product
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
data = cartData(self.request)
cartItems = data['cartItems']
context['cartItems'] = cartItems
return context
里面有什么response.context:
[
[
{'True': True, 'False': False, 'None': None},
{'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0x7fd80>>,
'request': <WSGIRequest: GET '/1/'>,
'user': <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7fd820>>, '
perms': <django.contrib.auth.context_processors.PermWrapper object at 0x7fd80>,
'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x7fd8290>,
'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}
},
{},
{'object': <Product: Pen>,
'product': <Product: Pen>,
'view': <ecom.views.ProductDetailView object at 0x7fd8210>,
'cartItems': 0}
],
[
{'True': True, 'False': False, 'None': None},
{'csrf_token': <SimpleLazyObject: <function csrf.<locals>._get_val at 0x7fd8240>>,
'request': <WSGIRequest: GET '/1/'>,
'user': <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x7fd8250>>,
'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x7fd8250>,
'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x7fd8290>,
'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}
},
{},
{'object': <Product: Pen>,
'product': <Product: Pen>,
'view': <ecom.views.ProductDetailView object at 0x7fd8210>,
'cartItems': 0}
]
]
response.context 的类型:
<class 'django.template.context.RequestContext'>
Product.objects.get(id=1)里面的内容是:Pen
Product.objects.get(id=1) 的类型是:<class 'ecom.models.Product'>
我不明白为什么:
它在 self.assertEqual(response.context[0]['object'], Product.objects.get(id=1)) 中找到了 Product 对象,但是不在 self.assertIn(Product.objects.get(id=1), response.context[0]['object']) - 说 TypeError: argument of type 'Product' is not iterable
它也没有在 self.assertIn(Product.objects.get(id=1), response.context[0]) 中找到它 - 说“AssertionError: <产品:Pen> 在 [....response.context[0]...] 中找不到
的内容它也没有在 self.assertIn(Product.objects.get(id=1), response.context[0][3]) 中找到它 - 说“在 getitem 引发 KeyError(key), KeyError: 3"
如何使用 RequestContext class? JSON喜欢?
抱歉,我的问题有点混乱,只是想了解如何使用 RequestContext。 提前致谢!
我认为您的测试失败了,因为 assertIn
通过 KEYS 而不是值来查看。解决方案是:
self.assertIn(Product.objects.get(id=1), response.context[0].values())
多一点解释:response.context[0]
似乎是一些键值存储,即字典。当您执行 response.context[0]["object"]
时,您刚刚访问了键“对象”处的值,其中 response.context[0]
是字典。在字典上做一些 in
查询只会查找字典的键。