Friday, February 26, 2016

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



On Fri, Feb 26, 2016 at 2:27 PM, Malik Rumi <malik.a.rumi@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/CA%2Be%2BciX2hj%2BEqqx%3DtVVw3nyUiLbi%2BGNP5vW6ujNFL%2BO0ML-0FQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment