0

I get this error after followed an tutorial, and I'm pretty new to this, but cant figure out what is wrong, since i have checked, double checked, triple checked and so on. it's probably a simple error but for me as a beginner it's not simple ;)

Exception in thread django-main-thread: Traceback (most recent call last):   File

"c:\users\jenny\appdata\local\programs\python\python38-32\lib\threading.py", line 932, in _bootstrap_inner self.run() File "c:\users\jenny\appdata\local\programs\python\python38-32\lib\threading.py", line 870, in run self._target(*self._args, **self.kwargs) File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper fn(*args, **kwargs) File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\core\management\commands\runserver.py", line 124, in inner_run self.check(display_num_errors=True) File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\core\management\base.py", line 438, in check all_issues = checks.run_checks( File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\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\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config return check_resolver(resolver) File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver return check_method() File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\urls\resolvers.py", line 446, in check
for pattern in self.url_patterns: File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\utils\functional.py", line 48, in get res = instance.dict[self.name] = self.func(instance) File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\urls\resolvers.py", line 632, in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\utils\functional.py", line 48, in get res = instance.dict[self.name] = self.func(instance) File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\urls\resolvers.py", line 625, in urlconf_module return import_module(self.urlconf_name) File "c:\users\jenny\appdata\local\programs\python\python38-32\lib\importlib_init
.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1014, in _gcd_import File "", line 991, in _find_and_load File "", line 975, in _find_and_load_unlocked File "", line 671, in _load_unlocked
File "", line 783, in exec_module File "", line 219, in call_with_frames_removed File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\smb_erp\urls.py", line 21, in path('', include('purchase.urls')), File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\env\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module) File "c:\users\jenny\appdata\local\programs\python\python38-32\lib\importlib_init
.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1014, in _gcd_import File "", line 991, in _find_and_load File "", line 975, in _find_and_load_unlocked File "", line 671, in _load_unlocked
File "", line 783, in exec_module File "", line 219, in _call_with_frames_removed File "C:\Users\jenny\Documents\OneDrive Jens privat\OneDrive\Programmering\Django\Nymoen ERP project\purchase\urls.py", line 24, in path('pdf_view/', views.viewPDF.as_view(), name="pdf_view"), AttributeError: module 'purchase.views' has no attribute 'viewPDF'

views.py

class ViewPDF(View):
def get(self):

    pdf = render_to_pdf('pdf_template.html', data)
    return HttpResponse(pdf, content_type='application/pdf')

urls.py

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('admin/', admin.site.urls),
    
    path('register/', views.registerPage, name="register"),
    path('login/', views.loginPage, name="login"),
    path('logout/', views.logoutUser, name="logout"),
    
    path('', views.home, name="home"),
    path('supplier/<str:pk>/', views.supplier, name="supplier"),
    path('products/', views.products, name="products"),
    path('purchase_order/', views.purchase_order, name="purchase_order"),
    path('order_form/', views.createPurchaseOrder, name="create_purchase_order"),
    path('update_order/<str:pk>/', views.updatePurchaseOrder, name="update_order"),
    path('delete_order/<str:pk>', views.deletePurchaseOrder, name="delete_order"),
    path('suppliers/', views.suppliers_list, name="supplierslist"),
    path('create_supplier/', views.createSupplier, name="create_supplier"),
    path('update_supplier/<str:pk>/', views.updateSupplier, name="update_supplier"),
    path('create_product/', views.createProduct, name="create_Product"),
    path('warehouse_list/', views.warehouse, name="warehouse_list"),

    path('pdf_view/', views.viewPDF, name="pdf_view"),


4
  • are you importing files from sub folder ? can you please show full urls.py Commented Jan 13, 2022 at 14:57
  • I have update my urls.py now. Commented Jan 13, 2022 at 20:18
  • Your view is named ViewPDF and not viewPDF. Commented Jan 13, 2022 at 22:42
  • I changed, but still got the same error Commented Jan 13, 2022 at 23:35

1 Answer 1

0

From the official documentation

Because Django’s URL resolver expects to send the request and associated arguments to a callable function, not a class, class-based views have an as_view() class method which returns a function that can be called when a request arrives for a URL matching the associated pattern.

Change your urls.py from

path('pdf_view/',  views.viewPDF, name="pdf_view")

To this

path('pdf_view/',  views.viewPDF.as_view(), name="pdf_view")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.