为什么我无法访问在详细信息视图中从url字符串传递的kwargs ['party_id']?
Test url: http://127.0.0.1:8000/party/3/activity/2/
url(r'^party/(?P<party_id>\d*)/activity/(?P<pk>\d*)/$', ActivityView.as_view(), name='activity')
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)
答案 0 :(得分:5)
请尝试使用self.kwargs
。传递给kwargs
的{{1}}只是该方法的参数。