1

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)
Viktor
  • 4,218
  • 4
  • 32
  • 63
  • I don't know django, but that means that you should not be passing any arguments into the function. – Moon Cheesez Jul 04 '16 at 08:03
  • @MoonCheesez according to docs, I should pass those arguments https://github.com/c0ntribut0r/django-mptt-urls – Viktor Jul 04 '16 at 08:05
  • 1
    Please include a full traceback and a minimal but **complete** example when asking for debugging help. – Ilja Everilä Jul 04 '16 at 08:05
  • 1
    Show `activities.views.Category`. The error is raised when `mptt_urls` calls the view. [It passes 4 arguments](https://github.com/c0ntribut0r/django-mptt-urls/blob/master/test_project/gallery/views.py#L7): request, path, instance, extra. In other words: you probably should be passing it a function accepting those arguments instead of a class. – Ilja Everilä Jul 04 '16 at 08:12
  • @IljaEverilä is there any way I can adapt my code without deleting of class? – Viktor Jul 04 '16 at 08:20
  • 1
    From reading [django docs on `TemplateView`](https://docs.djangoproject.com/en/1.10/ref/class-based-views/base/#templateview) that's not even how you'd pass a class based `TemplateView` in urls conf. You'd pass an instance generated with [`as_view`](https://docs.djangoproject.com/en/1.10/ref/class-based-views/base/#django.views.generic.base.View.as_view) class method. – Ilja Everilä Jul 04 '16 at 08:25
  • Looks like `django-mptt-urls` doesn't do class-based views. Since it isn't [that big](https://github.com/c0ntribut0r/django-mptt-urls/blob/master/mptt_urls/__init__.py), you might want to adapt it (and maybe issue a pull request upstream). – dhke Jul 04 '16 at 08:57
  • 2
    @ViktorDanilov I'm not sure why your answer was downvoted or why you deleted it - it explained how to get `mptt_urls` to work. The answer you have accepted shows how to use class based/function based views in general -- it doesn't show how to use them with the `mptt_urls` package. – Alasdair Jul 04 '16 at 09:52

2 Answers2

2

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)
        }
    )
1

I don't know what you are trying to do this with.

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

There is two way to render a response class based view and function based view. But you are defining urls for functions and in view you are using Class based view that's the reason you are getting error.

How to solve this

  1. Class based view Change urls something like

    url(r'^(?P<path>.*)/', Category.as_view(), slug_field='slug')),
    

    And views would be.

    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
    
  2. Function based view Urls.py

    url(r'^(?P<path>.*)/','activities.views.Category', slug_field='slug')),
    
    Views.py
    

    def category(request, path): 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)
        }
    )
    

Hope this helps.

burning
  • 2,426
  • 6
  • 25
  • 38