Friday, February 26, 2016

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

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)),
]




On Friday, February 26, 2016 at 6:55:17 PM UTC-6, James Schneider wrote:


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
 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/bd017ea3-846f-48d8-8f1f-bcfe01f81903%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment