Невозможно передать аргументы в классе python3 - PullRequest
0 голосов
/ 03 апреля 2020

Во-первых, я создал класс с __init__() в models.py, как вы можете видеть ниже. И когда я импортирую его в мой views.py, intellisense vscode также показывает, что мой класс принимает 4 аргумента. Но когда я пытаюсь запустить программу, она выдает ошибку, в которой говорится, что класс не принимает никаких аргументов. Любая помощь приветствуется, ты.

models.py:

from django.db import models

class destination:
    def __init__(self, name, desc, price, img):
        self.name = name
        self.desc = desc
        self.price = price
        self.img = img

views.py:

from django.shortcuts import render
from .models  import destination

dest1 = destination('Bali', 'Nice place to visit', 1000, 'destination_1.jpg')
dest2 = destination('Mumbai', 'The city that never sleeps', 500, 'destination_2.jpg')
dest3 = destination('Sylhet', 'The city of tea', 100, 'destination_3.jpg')
dests = [dest1, dest2, dest3]

def travello(req):
    return render(req, 'index.html', {'dests': dests})

Когда я запускаю python3 manage.py runserver:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 95, in handle
    self.run(**options)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/base.py", line 390, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/management/base.py", line 377, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/urls/resolvers.py", line 398, in check
    for pattern in self.url_patterns:
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/urls/resolvers.py", line 571, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/urls/resolvers.py", line 564, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/adnanbadshah/telusko/telusko/urls.py", line 21, in <module>
    path('', include('travello.urls'))
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/adnanbadshah/telusko/travello/urls.py", line 1, in <module>
    from . import views
  File "/home/adnanbadshah/telusko/travello/views.py", line 6, in <module>
    dest1 = destination('Bali', 'Nice place to visit', 1000, 'destination_1.jpg')
TypeError: destination() takes no arguments

  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/utils/autoreload.py", line 579, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/utils/autoreload.py", line 564, in start_django
    reloader.run(django_main_thread)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/utils/autoreload.py", line 272, in run
    get_resolver().urlconf_module
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/utils/functional.py", line 80, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/urls/resolvers.py", line 564, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/adnanbadshah/telusko/telusko/urls.py", line 21, in <module>
    path('', include('travello.urls'))
  File "/home/adnanbadshah/test/lib/python3.7/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/adnanbadshah/telusko/travello/urls.py", line 1, in <module>
    from . import views
  File "/home/adnanbadshah/telusko/travello/views.py", line 6, in <module>
    dest1 = destination('Bali', 'Nice place to visit', 1000, 'destination_1.jpg')
TypeError: destination() takes no arguments

1 Ответ

1 голос
/ 03 апреля 2020

Выполнение вашего Кода в разрезе, как будто это работает для меня, и в этом случае код не указывает на проблему, поэтому я ожидаю, что вы импортируете другой пункт назначения в какой-то момент, который вы не указали в своем фрагменте или скрыты за другим импортом , Я бы порекомендовал переименовать ваш класс, например, Destination (использование заглавной буквы в любом случае рекомендуется для написания имен классов) и посмотреть, решит ли это вашу проблему.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...