'ImportError: cannot import name 'ProductDetails' from 'pages.views'
I have written test case for views for details page. Here is below code I have written, When I am running Pytest in terminal it raising these error. My project structure is as below
path('ProductDetails/<int:id>', views.onClickSearch.ProductDetails, name='ProductDetails'),
class onClickSearch():
def ProductDetails(request, id):
try:
email = request.session.get('email')
proddtls = vk_master_table.objects.filter(id=id).first()
if proddtls:
banners = Extras().bestDeals_cat(proddtls.category_desc)
# print(banners.product_name)
except Exception as e:
logging.error(e)
return render(request, "ProductDetails.html", {'error': error, 'form': HomeForm(),
'time': settings.SESSION_IDLE_TIMEOUT,
'name': first_last_initial(email),
'msg_count': msg_count_cl(email),
'fullname': fullname(email), 'ProductDetails': proddtls,
'signinForm': SigninForm(), 'signupForm': CustomerForm()})
return render(request, "ProductDetails.html", {'ProductDetails': proddtls, 'fbanners': banners,
'form': HomeForm(), 'signinForm': SigninForm(),
'name': first_last_initial(email),
'msg_count': msg_count_cl(email),
'fullname': fullname(email),
'time': settings.SESSION_IDLE_TIMEOUT,
'signupForm': CustomerForm()})
from django.test import RequestFactory
from django.urls import reverse
from django.contrib.auth.models import User
from pages.models import vk_customer
from mixer.backend.django import mixer
import pytest
from pages.views import ProductDetails
@pytest.mark.django_db
class TestViews:
def test_product_detail_authenticated(self):
mixer.blend('pages.vk_master_table')
path = reverse('detail', kwargs={'pk': 1516})
request = RequestFactory().get(path)
request.user = mixer.blend(vk_customer)
response = ProductDetails(request, pk=1516)
print(response)
assert response.status_code == 200
This the error i am getting.....
____________________________________________________________ ERROR collecting pages/tests/test_views.py ____________________________________________________________
ImportError while importing test module 'H:\Project\mysite\pages\tests\test_views.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
c:\users\user\appdata\local\programs\python\python38\lib\importlib\__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
pages\tests\test_views.py:8: in <module>
from pages.views import ProductDetails
E ImportError: cannot import name 'ProductDetails' from 'pages.views' (H:\Project\mysite\pages\views.py)
Please help me out to solve this error.
Thank you ~Salima
Solution 1:[1]
I have created a class named onClickSearch with method ProductDetails . So I can't import a method of a class. To use the method I need to import class. So in place of importing from pages.views import ProductDetails in test_views.py did from pages.views import onClickSearch & then use it's method as onClickSearch.
Solution 2:[2]
This is my views.py of ProductDetails.
def ProductDetails(request, id):
try:
email = request.session.get('email')
proddtls = vk_master_table.objects.filter(id=id).first()
if proddtls:
banners = Extras().bestDeals_cat(proddtls.category_desc)
# print(banners.product_name)
except Exception as e:
logging.error(e)
return render(request, "ProductDetails.html", {'error': error, 'form': HomeForm(),
'time': settings.SESSION_IDLE_TIMEOUT,
'name': first_last_initial(email),
'msg_count': msg_count_cl(email),
'fullname': fullname(email), 'ProductDetails': proddtls,
'signinForm': SigninForm(), 'signupForm': CustomerForm()})
return render(request, "ProductDetails.html", {'ProductDetails': proddtls, 'fbanners': banners,
'form': HomeForm(), 'signinForm': SigninForm(),
'name': first_last_initial(email),
'msg_count': msg_count_cl(email),
'fullname': fullname(email),
'time': settings.SESSION_IDLE_TIMEOUT,
'signupForm': CustomerForm()})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | sonu |
Solution 2 | sonu |