Django:如何处理添加的参数?

Django: how to handle added parameters?

文档很好here

他们给了我们这个例子:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^blog/(?P<year>[0-9]{4})/$', views.year_archive, {'foo': 'bar'}),
]

但他们并没有准确说明如何在视图代码中获取它!

在我这里的索引视图中,在哪里可以读取上面给出的参数 foo

class IndexView(generic.ListView):
    template_name = 'produits/index.html'
    context_object_name = 'liste_produits'

    def get_queryset(self):
        return Produit.objects.order_by('-date_v_fin', '-date_v_debut')[:5]

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        locale = translation.get_language()

self.kwargs['year'] 对于 URL 中的 year 变量本身就可以了。 self.kwargs['foo'] 将在最后获取该字典中传递的额外数据。一般来说,self.kwargs['name']会把变量name传给URL。 Documentation link