errors while trying to add extra context (detailview, __getitem__)

61 views
Skip to first unread message

Malik Rumi

unread,
Feb 26, 2016, 5:27:47 PM2/26/16
to Django users
File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 23, in <module>
    url(r'^code/(?P<slug>)/', CodeDetailView.as_view(), name='family'),
TypeError: 'function' object has no attribute '__getitem__'

I understand that DetailView does not have __getitem__. I also understand that __getitem__ is used for iteration. What I don't understand is why I am getting this error, or what to do about it. DetailView, which is supposed to be for a single object, so it must have some 'get' mechanism, and I see from ccbv.co.uk that it does, but then why is this traceback complaining about __getitem__? In other words, why is it looking for __getitem__ at all?

class CodeDetailView(DetailView):

    model = Code
    def get_context_data(self, **kwargs):
        context = super(CodeDetailView, self).get_context_data(**kwargs)
        template_name='statute_single.html'
        return context
        
Is this error really saying that I can't use DetailView where iteration is called for (as it is in my template)?

template

    <li><a href={% url 'family' 'slug' %}>{{ set_list }}</a></li>
    
I was trying to follow the code example from the docs, here:


(I know the extra context is not in the example posted)

James Schneider

unread,
Feb 26, 2016, 7:55:17 PM2/26/16
to django...@googlegroups.com
On Fri, Feb 26, 2016 at 2:27 PM, Malik Rumi <malik....@gmail.com> wrote:
File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 23, in <module>
    url(r'^code/(?P<slug>)/', CodeDetailView.as_view(), name='family'),
TypeError: 'function' object has no attribute '__getitem__'

I understand that DetailView does not have __getitem__. I also understand that __getitem__ is used for iteration. What I don't understand is why I am getting this error, or what to do about it. DetailView, which is supposed to be for a single object, so it must have some 'get' mechanism, and I see from ccbv.co.uk that it does, but then why is this traceback complaining about __getitem__? In other words, why is it looking for __getitem__ at all?

Can you post the entire section for your urls.py, as well as the entire traceback? Nothing is immediately jumping out at me. Do you get this error when you start the Django server, or when you visit the page that matches this URL?
 
class CodeDetailView(DetailView):

    model = Code
    def get_context_data(self, **kwargs):
        context = super(CodeDetailView, self).get_context_data(**kwargs)
        template_name='statute_single.html'
        return context

Why is your template_name here in get_context_data? Normally that goes in the class definition.

Also, this override for get_context_data() doesn't actually do anything. You need to modify the context variable and then return it in order to add things to the template context.
 
        
Is this error really saying that I can't use DetailView where iteration is called for (as it is in my template)?

template

    <li><a href={% url 'family' 'slug' %}>{{ set_list }}</a></li>
    


I don't see any portion of your code where iteration is actually used, so I'm not sure where/why you are referencing it. The variable {{ set_list }} is likely not available because your get_context_data() override never added it. 

 
I was trying to follow the code example from the docs, here:


(I know the extra context is not in the example posted)

You mean the variables that you are adding to the context are not included in the method override you posted above? That would explain my confusion about the get_context_data() override not doing anything.

On the whole, I suspect an issue somewhere in your urls.py file, but I don't see anything immediately wrong with the single line you've posted.

-James
 

Malik Rumi

unread,
Feb 26, 2016, 8:49:23 PM2/26/16
to Django users
Hi James, thanks for replying and I hope you are still online tonight, because I just got this same error again, but this time for my admin:

I've been working on this particular project for weeks and it has never complained about this config.

  File "/home/malikarumi/Projects/cannon/jamf/jamf/urls.py", line 15, in <module>
    url(r'^admin/', include(admin.site.urls)),
TypeError: 'function' object has no attribute '__getitem__'

I was trying a workaround to the first error I posted by following https://docs.djangoproject.com/en/1.9/topics/class-based-views/mixins/

#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.conf.urls import include, url, patterns
from django.contrib import admin
from . import views
from django.views.generic.base import TemplateView
from essell.views import CodeListViewAll



urlpatterns = patterns['',
    
    url(r'^$', TemplateView.as_view(template_name='home.html')),
    url(r'^library/', include('essell.urls')),
    url(r'^admin/', include(admin.site.urls)),
]

Malik Rumi

unread,
Feb 26, 2016, 8:55:06 PM2/26/16
to Django users
And here is the code from the original error:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from django.conf.urls import include, url, patterns
from . import views
from django.views.generic.base import TemplateView
from essell.models import Code
from essell.views import CodeListViewAll
#from essell.views import CodeDetailView
from essell.views import CodeDetail


urlpatterns = patterns(['',
    url(r'^$', TemplateView.as_view(template_name='library.html')),
    url(r'code/', CodeDetail.as_view(template_name='code_family_detail')),
    url(r'^constitution', CodeListViewAll.as_view(), name='constitution'), 
    # this is the one that got the error)
    url(r'^code/(?P<slug>[\w-]+)/$', CodeDetailView.as_view(), name='family'),
])

'''
for convenience sake, here is the error again:
 File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 23, in <module>
    url(r'^code/(?P<slug>[\w-]+)/$', CodeDetailView.as_view(), name='family'),
'''

Malik Rumi

unread,
Feb 26, 2016, 9:04:57 PM2/26/16
to Django users
JAMES wrote:  >>> Can you post the entire section for your urls.py, as well as the entire traceback? Nothing is immediately jumping out at me. Do you get this error when you start the Django server, or when you visit the page that matches this URL?

This error comes when I try to start the server.

I am starting the traceback here because at first I had it as a free standing module and not inside views.py, but i thought they said a view is a callable and so can be anywhere and be named anything?

 File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 14, in <module>
    url(r'code/', CodeDetail.as_view(template_name='code_family_detail')),
AttributeError: 'module' object has no attribute 'as_view'

Why? It is clearly subclassed from list view. I got it straight from the docs


Then I put it in views.py. Now I see an unhandled exception, but I don't know what it means or what to do about it.

 File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 9, in <module>
    from essell.views import CodeDetail
ImportError: cannot import name CodeDetail
Unhandled exception in thread started by <function wrapper at 0x7f85a2b7e230>
Traceback (most recent call last):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 228, in patch_all
    patch_root_urlconf()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 216, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 568, in reverse
    app_list = resolver.app_dict[ns]
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 360, in app_dict
    self._populate()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 293, in _populate
    for pattern in reversed(self.url_patterns):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/malikarumi/Projects/cannon/jamf/jamf/urls.py", line 14, in <module>
    url(r'^library/', include('essell.urls')),
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 14, in <module>
    url(r'code/', CodeDetail.as_view(template_name='code_family_detail')),
AttributeError: 'module' object has no attribute 'as_view'
Unhandled exception in thread started by <function wrapper at 0x7f8f9b061410>
Traceback (most recent call last):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 228, in patch_all
    patch_root_urlconf()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 216, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 568, in reverse
    app_list = resolver.app_dict[ns]
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 360, in app_dict
    self._populate()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 293, in _populate
    for pattern in reversed(self.url_patterns):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/malikarumi/Projects/cannon/jamf/jamf/urls.py", line 14, in <module>
    url(r'^library/', include('essell.urls')),
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 14, in <module>
    url(r'code/', CodeDetail.as_view(template_name='code_family_detail')),
AttributeError: 'module' object has no attribute 'as_view'
Unhandled exception in thread started by <function wrapper at 0x7f4249981410>
Traceback (most recent call last):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 228, in patch_all
    patch_root_urlconf()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 216, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 568, in reverse
    app_list = resolver.app_dict[ns]
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 360, in app_dict
    self._populate()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 293, in _populate
    for pattern in reversed(self.url_patterns):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/malikarumi/Projects/cannon/jamf/jamf/urls.py", line 14, in <module>
    url(r'^library/', include('essell.urls')),
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 14, in <module>
    url(r'code/', CodeDetail.as_view(template_name='code_family_detail')),
AttributeError: 'module' object has no attribute 'as_view'
Unhandled exception in thread started by <function wrapper at 0x7fddcc7ed410>
Traceback (most recent call last):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 228, in patch_all
    patch_root_urlconf()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 216, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 568, in reverse
    app_list = resolver.app_dict[ns]
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 360, in app_dict
    self._populate()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 293, in _populate
    for pattern in reversed(self.url_patterns):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/malikarumi/Projects/cannon/jamf/jamf/urls.py", line 14, in <module>
    url(r'^library/', include('essell.urls')),
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 14, in <module>
    url(r'code/', CodeDetail.as_view(template_name='code_family_detail')),
AttributeError: 'module' object has no attribute 'as_view'
/home/malikarumi/Projects/cannon/jamf/essell/urls.py:24: RemovedInDjango110Warning: django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead.
  url(r'^constitution', CodeListViewAll.as_view(), name='constitution'), #(2)

Unhandled exception in thread started by <function wrapper at 0x7f4d279d18c0>
Traceback (most recent call last):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/apps.py", line 15, in ready
    dt_settings.patch_all()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 228, in patch_all
    patch_root_urlconf()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/debug_toolbar/settings.py", line 216, in patch_root_urlconf
    reverse('djdt:render_panel')
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 568, in reverse
    app_list = resolver.app_dict[ns]
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 360, in app_dict
    self._populate()
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 293, in _populate
    for pattern in reversed(self.url_patterns):
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 417, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/malikarumi/Projects/cannon/local/lib/python2.7/site-packages/django/core/urlresolvers.py", line 410, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/malikarumi/Projects/cannon/jamf/jamf/urls.py", line 15, in <module>
    url(r'^admin/', include(admin.site.urls)),

Malik Rumi

unread,
Feb 26, 2016, 9:38:42 PM2/26/16
to Django users
OK, so maybe....MAYBE I've got this figured out. The problem is here:

https://docs.djangoproject.com/en/1.9/ref/urls/#django.conf.urls.url


I knew this, but note the examples still use the parens()


https://docs.djangoproject.com/en/1.9/ref/urls/#patterns


but that's what patterns is. It is a function. So it should have the parens. The problem is not the parens, but patterns() itself.


HOWEVER, I still have issues, if there are any night owls or people on the other side of the world who can help:

error msg:
Generic detail view CodeDetail must be called with either an object pk or a slug.
but I copied this straight out of the docs! All I did was changed the app names! See https://docs.djangoproject.com/en/1.9/topics/class-based-views/mixins/ 

"We’ll rely on the default implementation of get_object() to fetch the correct Publisher object. However, we need to explicitly pass a queryset argument because otherwise the default implementation of get_object() would call get_queryset() which we have overridden to return Book objects instead of Publisher ones. " 

James Schneider

unread,
Feb 26, 2016, 11:34:16 PM2/26/16
to django...@googlegroups.com


On Feb 26, 2016 5:55 PM, "Malik Rumi" <malik....@gmail.com> wrote:
>
> And here is the code from the original error:
>
> #!/usr/bin/python
> # -*- coding: utf-8 -*-
> from django.conf.urls import include, url, patterns
> from . import views
> from django.views.generic.base import TemplateView
> from essell.models import Code
> from essell.views import CodeListViewAll
> #from essell.views import CodeDetailView
> from essell.views import CodeDetail
>
>
> urlpatterns = patterns(['',
>     url(r'^$', TemplateView.as_view(template_name='library.html')),
>     url(r'code/', CodeDetail.as_view(template_name='code_family_detail')),

You've got CodeDetail instead of CodeDetailView, is that correct? That's not the class that you posted earlier.

-James

James Schneider

unread,
Feb 27, 2016, 4:23:30 AM2/27/16
to django...@googlegroups.com
SingleObjectMixin looks for URL kwargs named 'pk' or 'slug' by default. There's been a fair bit of cross-talk about which URL's are actually the problem, but you only have one that would work for the view you are trying to call: url(r'^code/(?P<slug>[\w-]+)/$', CodeDetailView.as_view(), name='family'),

At this point I can't tell which URL you are talking about, so I don't know if you are executing/matching the right one. The error would indicate that you are matching just r'code/' without the (?P<slug>), so the view has no way to look up the correct object to return. 

-James

James Schneider

unread,
Feb 27, 2016, 4:26:53 AM2/27/16
to django...@googlegroups.com

> urlpatterns = patterns(['',
>     url(r'^$', TemplateView.as_view(template_name='library.html')),
>     url(r'code/', CodeDetail.as_view(template_name='code_family_detail')),

You've got CodeDetail instead of CodeDetailView, is that correct? That's not the class that you posted earlier.


Additionally, "detail" URL's typically contain a PK or a slug match, because you need to know which object you want to find the details on. The last URL you have listed here is more akin to a typical ListView. 

Additionally to my addition, you are also lacking a $ at the end of the regex, which means that your other views that do contain the (?P<slug>) match will never hit, since 'code/' will match those URL's first before ever reaching your true detail view. Probably not what you want.

-James

James Schneider

unread,
Feb 27, 2016, 4:31:15 AM2/27/16
to django...@googlegroups.com
On Fri, Feb 26, 2016 at 6:04 PM, Malik Rumi <malik....@gmail.com> wrote:
JAMES wrote:  >>> Can you post the entire section for your urls.py, as well as the entire traceback? Nothing is immediately jumping out at me. Do you get this error when you start the Django server, or when you visit the page that matches this URL?

This error comes when I try to start the server.

I am starting the traceback here because at first I had it as a free standing module and not inside views.py, but i thought they said a view is a callable and so can be anywhere and be named anything?

 File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 14, in <module>
    url(r'code/', CodeDetail.as_view(template_name='code_family_detail')),
AttributeError: 'module' object has no attribute 'as_view'

Why? It is clearly subclassed from list view. I got it straight from the docs


Then I put it in views.py. Now I see an unhandled exception, but I don't know what it means or what to do about it.

 File "/home/malikarumi/Projects/cannon/jamf/essell/urls.py", line 9, in <module>
    from essell.views import CodeDetail
ImportError: cannot import name CodeDetail
Unhandled exception in thread started by <function wrapper at 0x7f85a2b7e230>

Did you catch the ImportError here? Everything else is going to tumble until that gets fixed. 

I noticed that you use both CodeDetail and CodeDetailView. Near as I can tell, they are both supposed to be the same (or one was created as part of the troubleshooting process here). 

I would highly, highly recommend that you standardize on one or the other (my preference is the latter). It seems like you may be dealing with two different views and getting the names crossed.

-James
Reply all
Reply to author
Forward
0 new messages