Viktor Danilov Viktor Danilov - 2 months ago 25
Python Question

TypeError: __init__() takes exactly 1 argument (4 given)

mptt-urls
raises:


TypeError at /activities/test/

__init__() takes exactly 1 argument (4 given)



This is my url pattern:

url(r'^(?P<path>.*)/', mptt_urls.view(model='activities.models.Category',
view='activities.views.Category', slug_field='slug')),


And my view:

class Category(TemplateView):
template_name = 'activities/articles_list.html'

def get_context_data(self, **kwargs):
c = super(Category, self).get_context_data(**kwargs)
c['articles'] = models.Article.objects.filter(Category=self)
return c


What am I doing wrong?




Traceback:



Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/activities/test/

Django Version: 1.9.7
Python Version: 2.7.11
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'taggit',
'mptt',
'ckeditor',
'easy_thumbnails',
'activities',
'common',
'images']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)

File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/dukeimg/PycharmProjects/dorogi/env/local/lib/python2.7/site-packages/mptt_urls/__init__.py" in __call__
38. return self.view(*args, **kwargs)

Exception Type: TypeError at /activities/test/
Exception Value: __init__() takes exactly 1 argument (4 given)

Answer

This may work for you:

def category(request, path, instance):
    l = get_list_or_404(models.Category.objects.all(), slug=path)

    return render(
        request,
        'activities/activity_list.html',
        {
            'articles': l,
            'activities': models.Category.objects.filter(level__lte=0)
        }
    )