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

时间:2015-07-31 22:01:18

标签: django django-views

文档很好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()

1 个答案:

答案 0 :(得分:2)

对于URL本身中的year变量,

self.kwargs['foo']可以解决问题。 self.kwargs['name']将在结尾处获取该字典中传递的额外数据。通常,name会将变量 hidden: hides everything what is not inside the area, no scrollbars. overlay: shows scrollbars if needed and overlays scrollbars top of contents scroll: show scrollbars normally visible: shows all the child elements of current content, no area limitations. 传入URL。 Documentation link