为什么Django DetailView没有可用的kwargs

时间:2013-09-20 16:00:14

标签: python django django-class-based-views class-based-views

为什么我无法访问在详细信息视图中从url字符串传递的kwargs ['party_id']?

Test url: http://127.0.0.1:8000/party/3/activity/2/

urls.py

url(r'^party/(?P<party_id>\d*)/activity/(?P<pk>\d*)/$', ActivityView.as_view(), name='activity')

views.py

class ActivityView(DetailView):
    template_name = "ooe/activity_results.html"
    context_object_name = "activity"
    model = Activity

    def get_context_data(self, **kwargs):
        context = super(ActivityView, self).get_context_data(**kwargs)
        #this line is not working because kwargs is just: {'object': <Activity: Activity 2>}
        context['submissions'] = self.get_object().get_submissions(kwargs['party_id'])       
        return context

    @method_decorator(user_passes_test(active_participant))
    def dispatch(self, *args, **kwargs):
        return super(ActivityView, self).dispatch(*args, **kwargs)

1 个答案:

答案 0 :(得分:5)

请尝试使用self.kwargs。传递给kwargs的{​​{1}}只是该方法的参数。