django类视图有一个错误:'get_context_data()关键字必须是string '

时间:2022-08-17 19:22:24

I am doing migration on a django site, I rewrote the list_object view to class based view which inheritated from django.views.generic.ListView, but I got error when I create get_context_data method

我正在django站点上进行迁移,我将list_object视图重写为继承自django.views.generic的基于类的视图。ListView,但创建get_context_data方法时出错

My code here:

我的代码:

class ForumView(ListView):
    context_object_name = 'forum'
    template_name = 'forum/thread_list.html',
    paginate_by = FORUM_PAGINATION
    def get_queryset(self):
        try:
            f = Forum.objects.for_groups(self.request.user.groups.all()).select_related().get(slug=self.kwargs['slug'])
        except Forum.DoesNotExist:
            raise Http404
        return f.thread_set.select_related().all()
    def get_context_data(self, **kwargs):
        try:
            f = Forum.objects.for_groups(self.request.user.groups.all()).select_related().get(slug=self.kwargs['slug'])
        except Forum.DoesNotExist:
            raise Http404
        form = CreateThreadForm()
        child_forums = f.child.for_groups(self.request.user.groups.all())
        extra_context = {
            'forum': f,
            'child_forums': child_forums,
            'form': form,
            'login': {
                'reason': _('create a new thread'),
                'next': f.get_absolute_url(),
            },
            'section': 'forum',
        }
        context = super(ForumView, self).get_context_data(**kwargs)
        for key in extra_context:
            context[key] = extra_context[key]
        return context

and url.py

和url.py

url(r'^thread/(?P<thread>[0-9]+)/$', ThreadView.as_view(), name='forum_view_thread'),

but the django return a debug page with:

但是django返回一个调试页面:

Exception Value: get_context_data() keywords must be strings
Python Version: 2.7.3
Django Version: 1.5.2
Request URL: http://localhost:8000/forum/thread/1/
Exception Location: /Library/Python/2.7/site-packages/django/views/generic/list.py in get_context_data, line 116

traceback:
/Library/Python/2.7/site-packages/django/core/handlers/base.py in get_response
                    response = callback(request, *callback_args, **callback_kwargs) ...

/Library/Python/2.7/site-packages/django/views/generic/base.py in view
        return self.dispatch(request, *args, **kwargs) ...

/Library/Python/2.7/site-packages/django/views/generic/base.py in dispatch
    return handler(request, *args, **kwargs) ...

/Library/Python/2.7/site-packages/django/views/generic/list.py in get
    context = self.get_context_data(object_list=self.object_list) ...

/Users/foulwall/Dropbox/mldata/forum/views.py in get_context_data
    context = super(ThreadView, self).get_context_data(**kwargs) ...

/Library/Python/2.7/site-packages/django/views/generic/list.py in get_context_data
    return super(MultipleObjectMixin, self).get_context_data(**context) ...

Anyone with a solution? thanks~

任何一个有解决方案吗?谢谢~

1 个解决方案

#1


1  

I suspect that one of the kwargs that you are sending to get_context_data is not a simple string object. This could be passed through as the key in a kwargs dictionary. Perhaps it is a lazy or a translatable string?

我怀疑您发送给get_context_data的kwargs不是一个简单的字符串对象。这可以作为kwargs字典中的关键字传递。也许是一个懒字符串还是一个可翻译的字符串?

Something similar happened to the person that asked this question. If it the same issue, I suggest you ensure that the variable name is indeed a simple string. Unicode chars in variable names are odd, but acceptable btw.

问这个问题的人也发生了类似的事情。如果是相同的问题,我建议您确保变量名确实是一个简单的字符串。变量名中的Unicode字符是奇数,但顺便说一句,是可以接受的。

#1


1  

I suspect that one of the kwargs that you are sending to get_context_data is not a simple string object. This could be passed through as the key in a kwargs dictionary. Perhaps it is a lazy or a translatable string?

我怀疑您发送给get_context_data的kwargs不是一个简单的字符串对象。这可以作为kwargs字典中的关键字传递。也许是一个懒字符串还是一个可翻译的字符串?

Something similar happened to the person that asked this question. If it the same issue, I suggest you ensure that the variable name is indeed a simple string. Unicode chars in variable names are odd, but acceptable btw.

问这个问题的人也发生了类似的事情。如果是相同的问题,我建议您确保变量名确实是一个简单的字符串。变量名中的Unicode字符是奇数,但顺便说一句,是可以接受的。