Hi all,
I have written test case for ProductDetails_ls for views
views.py
```
class onClickSearch():
def ProductDetails_ls(request, id):
# product_ls = vk_ls_product_search.objects.get(id=id)
product_ls = get_object_or_404(vk_ls_product_search, id=id)
email = request.session.get('email')
return render(request, "productdetails_ls.html", {
'product_ls': product_ls,'msg_count': msg_count_cl(email),
'time': settings.SESSION_IDLE_TIMEOUT,
'name': first_last_initial(email),
'fullname': fullname(email),
})
```
urls.py
```
path('ProductDetails_ls/<int:id>', views.onClickSearch.ProductDetails_ls, name='ProductDetails_ls'),
```
test_views.py
```
@pytest.mark.django_db
class TestViews:
def test_product_detail_ls_authenticated(self):
mixer.blend('pages.vk_ls_product_search')
path = reverse('ProductDetails_ls', kwargs={'id': 19})
request = RequestFactory().get(path)
request.user = mixer.blend(vk_customer)
response = onClickSearch.ProductDetails_ls(request, id=19)
print(response)
assert response.status_code == 200
```
Here, I am getting this error please anyone can help me out to solve this error,
```
============================================================================= FAILURES =============================================================================
__________________________________________________________ TestViews.test_product_detail_ls_authenticated __________________________________________________________
klass = <class 'pages.models.vk_ls_product_search'>, args = (), kwargs = {'id': 19}, queryset = <QuerySet [<vk_ls_product_search: HmuKHxkrbmhWDylFcBOd>]>
def get_object_or_404(klass, *args, **kwargs):
"""
Use get() to return an object, or raise a Http404 exception if the object
does not exist.
klass may be a Model, Manager, or QuerySet object. All other passed
arguments and keyword arguments are used in the get() query.
Like with QuerySet.get(), MultipleObjectsReturned is raised if more than
one object is found.
"""
queryset = _get_queryset(klass)
if not hasattr(queryset, 'get'):
klass__name = klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
raise ValueError(
"First argument to get_object_or_404() must be a Model, Manager, "
"or QuerySet, not '%s'." % klass__name
)
try:
> return queryset.get(*args, **kwargs)
c:\users\user\appdata\local\programs\python\python38\lib\site-packages\django\shortcuts.py:76:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <QuerySet [<vk_ls_product_search: HmuKHxkrbmhWDylFcBOd>]>, args = (), kwargs = {'id': 19}, clone = <QuerySet []>, limit = 21, num = 0
def get(self, *args, **kwargs):
"""
Perform the query and return a single object matching the given
keyword arguments.
"""
clone = self._chain() if self.query.combinator else self.filter(*args, **kwargs)
if self.query.can_filter() and not self.query.distinct_fields:
clone = clone.order_by()
limit = None
if not clone.query.select_for_update or connections[clone.db].features.supports_select_for_update_with_limit:
limit = MAX_GET_RESULTS
clone.query.set_limits(high=limit)
num = len(clone)
if num == 1:
return clone._result_cache[0]
if not num:
> raise self.model.DoesNotExist(
"%s matching query does not exist." %
self.model._meta.object_name
)
E pages.models.vk_ls_product_search.DoesNotExist: vk_ls_product_search matching query does not exist.
c:\users\user\appdata\local\programs\python\python38\lib\site-packages\django\db\models\query.py:415: DoesNotExist
During handling of the above exception, another exception occurred:
self = <test_views.TestViews object at 0x0000002030703EE0>
def test_product_detail_ls_authenticated(self):
mixer.blend('pages.vk_ls_product_search')
path = reverse('ProductDetails_ls', kwargs={'id': 19})
request = RequestFactory().get(path)
request.user = mixer.blend(vk_customer)
> response = onClickSearch.ProductDetails_ls(request, id=19)
pages\tests\test_views.py:32:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
pages\views.py:2795: in ProductDetails_ls
product_ls = get_object_or_404(vk_ls_product_search, id=id)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
klass = <class 'pages.models.vk_ls_product_search'>, args = (), kwargs = {'id': 19}, queryset = <QuerySet [<vk_ls_product_search: HmuKHxkrbmhWDylFcBOd>]>
def get_object_or_404(klass, *args, **kwargs):
"""
Use get() to return an object, or raise a Http404 exception if the object
does not exist.
klass may be a Model, Manager, or QuerySet object. All other passed
arguments and keyword arguments are used in the get() query.
Like with QuerySet.get(), MultipleObjectsReturned is raised if more than
one object is found.
"""
queryset = _get_queryset(klass)
if not hasattr(queryset, 'get'):
klass__name = klass.__name__ if isinstance(klass, type) else klass.__class__.__name__
raise ValueError(
"First argument to get_object_or_404() must be a Model, Manager, "
"or QuerySet, not '%s'." % klass__name
)
try:
return queryset.get(*args, **kwargs)
except queryset.model.DoesNotExist:
> raise Http404('No %s matches the given query.' % queryset.model._meta.object_name)
E django.http.response.Http404: No vk_ls_product_search matches the given query.
c:\users\user\appdata\local\programs\python\python38\lib\site-packages\django\shortcuts.py:78: Http404
```
But I have that model in my models.py
models.py
```
class vk_ls_product_search(models.Model):
# linkid = models.IntegerField(null="true")
merchantname = models.CharField(max_length=255, null="false")
product_name = models.CharField(max_length=255)
sku = models.CharField(max_length=300, null="false")
img_url = models.TextField(max_length=255, null="false")
description_short = models.TextField(max_length=255, null=True)
description_long = models.TextField(max_length=255, null=True)
price = models.DecimalField(max_digits=10, decimal_places=2, null="false", default=0)
sale_price = models.DecimalField(max_digits=10, decimal_places=2, null="false", default=0)
category_primary = models.CharField(max_length=255, null="false")
category_secondary = models.CharField(max_length=255, null="false")
createdon = models.DateTimeField(editable=False, null=True)
link_url = models.TextField(max_length=500, null=True)
upc_code = models.BigIntegerField(null=True)
rebate = models.BigIntegerField(null="false", default=0)
type_of_product = models.CharField(max_length=50, null=True, default="lsps")
webservice_id = models.IntegerField(null=True)
""" Here, it saves time as your timezone """
def save(self, *args, **kwargs):
""" On save, update timestamps """
if not
self.pk:
self.createdon = timezone.now()
return super(vk_ls_product_search, self).save(*args, **kwargs)
def __str__(self):
return self.product_name
class Meta:
verbose_name_plural = 'vk_ls_product_search'
```
Please, Help me out to solve this error.
Thank you
~Salima