Home > Software engineering >  one django project two apps, problem with generic views( class view)
one django project two apps, problem with generic views( class view)

Time:04-02

i have two apps in one project: store and basket.

In urls.py in core of project:

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('jewelry_store.urls', namespace='jewelry_store')),
    path('basket/', include('basket.urls', namespace='basket')), 
]

in urls.py in basket app :

from django.urls import path
from basket import views

app_name = 'basket'

urlpatterns = [
    path('', views.BasketSummary.as_view(), name='basket_summary')
]

in urls.py in jewelry_store app :


from django.urls import path

from jewelry_store.models import Category

from . import views

app_name = 'jewelry_store'

urlpatterns = [
    path('', views.AllListView.as_view(), name='all_products'),
    path('<slug:slug>', views.ProductView.as_view(), name='product_detail'),
    path('category/<slug:slug>/', views.CategoryView.as_view(), name='all_categories'),
    path('collection/<slug:slug>/', views.CollectionView.as_view(), name='all_collections'),
    
]

i wold like to have class based view in views.py, like this :

from msilib.schema import ListView
from django.shortcuts import render

import basket
from jewelry_store.models import Product

# Create your here.

class BasketSummary(ListView):
    model = Product
    template_name = "jewelry_store/basket/summary.html"

but it generate error like this

enter image description here

when it is done in functions mode everything is correct. What is proper configuration for class views ?

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\nasz\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\nasz\AppData\Local\Programs\Python\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\core\management\commands\runserver.py", line 123, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\core\management\base.py",
line 438, in check
    all_issues = checks.run_checks(
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\core\checks\registry.py",
line 77, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\urls\resolvers.py", line 445, in check
    for pattern in self.url_patterns:
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\urls\resolvers.py", line 631, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\utils\functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\urls\resolvers.py", line 624, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\nasz\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\nasz\dev\shop\core\urls.py", line 24, in <module>
    path('basket/', include('basket.urls', namespace='basket')),
  File "C:\Users\nasz\dev\shop\myvenv\lib\site-packages\django\urls\conf.py", line 33, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\nasz\AppData\Local\Programs\Python\Python38-32\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\nasz\dev\shop\basket\urls.py", line 2, in <module>
    from basket import views
  File "C:\Users\nasz\dev\shop\basket\views.py", line 9, in <module>
    class BasketSummary(ListView):
TypeError: __init__() takes 2 positional arguments but 4 were given

CodePudding user response:

Import ListView from django generic view module.

On top of your views.py file change this line

from msilib.schema import ListView

To

from django.views.generic

CodePudding user response:

try this:

class BasketSummary(ListView):
    model = Product
    template_name = "basket/summary.html"

instead of this:

class BasketSummary(ListView):
    model = Product
    template_name = "jewelry_store/basket/summary.html"
  • Related