Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
517 views
in Technique[技术] by (71.8m points)

python - Django client get: TypeError: __init__() takes 1 positional argument but 2 were given

I have following easy test:

class DeviceListTests(APITestCase):
    def test_devices_list(self):
        self.user = UserFactory()
        self.device = DeviceFactory(name="SoundTalks_device")
        self.client.force_login(self.user)
        response = self.client.get(
            reverse("device-list")
        )

Python/Django is returning an error on:

response = self.client.get(
                reverse("device-list")
            )

However this is an easy request, I get following error:

Traceback (most recent call last):
  File "/opt/project/integrator_api/devices/tests/test_get_devices.py", line 15, in test_devices_list
    reverse("device-list")
  File "/usr/local/lib/python3.6/site-packages/rest_framework/test.py", line 286, in get
    response = super().get(path, data=data, **extra)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/test.py", line 203, in get
    return self.generic('GET', path, **r)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/test.py", line 232, in generic
    method, path, data, content_type, secure, **extra)
  File "/usr/local/lib/python3.6/site-packages/django/test/client.py", line 422, in generic
    return self.request(**r)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/test.py", line 283, in request
    return super().request(**kwargs)
  File "/usr/local/lib/python3.6/site-packages/rest_framework/test.py", line 235, in request
    request = super().request(**kwargs)
  File "/usr/local/lib/python3.6/site-packages/django/test/client.py", line 503, in request
    raise exc_value
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
TypeError: __init__() takes 1 positional argument but 2 were given

app_1      | 
app_1      | Aborting on container exit...
Stopping integrator-api_app_1   ... 
Stopping integrator-api_app_1   ... done
Process finished with exit code 1

devices/urls.py

from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns

from integrator_api.devices.views.device_list_view import DeviceListView

urlpatterns = format_suffix_patterns(
    [
        url(r"^$", DeviceListView, name="device-list"),
    ]
)

integrator_api/urls.py (project urls)

from django.urls import path, include
from integrator_api.devices import urls as device_urls


urlpatterns = [
    path("devices/", include(device_urls)),
]
question from:https://stackoverflow.com/questions/65873437/django-client-get-typeerror-init-takes-1-positional-argument-but-2-were

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I guess your 'DeviceListView' is a class based view so you will need to call inside urlpatterns with 'as_view()' like this:

# urls.py
urlpatterns = format_suffix_patterns(
[
    url(r"^$", DeviceListView.as_view(), name="device-list"),
])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...