【发布时间】:2020-07-05 21:39:03
【问题描述】:
我尝试使用此 url 配置在 Django 中动态调用视图:
url(r'^niveau/(?P<niveau_id>\d+)/(?P<channel>\w+)/$', views.switcher , name='vue_niveau')
然后在我的 view.py 中,我有:
def switcher(request, niveau_id, channel):
if channel == 'VueNiveau':
return VueNiveau.as_view()(request)
它有效,因为我得到了良好的视图调用,但是当我尝试在我的 VueNiveau 上下文中获取 niveau_id 时:
class VueNiveau(generic.TemplateView):
template_name = 'cours/vue_niveau.html'
...
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
niveau = Niveau.objects.get(id=kwargs['niveau_id'])
context['niveau'] = niveau
我收到一个 KeyError,说 niveau_id 未定义...
没有 switcher 函数,一切正常,我可以获取 url 数据等......所以在 switcher 函数的调用和 get_context_data 之间似乎发生了一些事情...... p>
有人理解这种行为吗?
【问题讨论】:
标签: django django-views django-urls django-context