Pass the captured named regular expression to the URL dictionary in general view

I work with a general view in Django. I want to capture the named parameter of the group in the URL and pass the value to the dictionary of URL patterns. For example, in URLConf below, I want to write a value parent_slugto a URL and pass it to the value of the query dictionary like this:

urlpatterns = patterns('django.views.generic.list_detail',
    (r'^(?P<parent_slug>[-\w]+)$',
     'object_list',
     {'queryset':Promotion.objects.filter(category=parent_slug)},
     'promo_promotion_list',
    ),
                      )

Is it possible to do this in a single URLConf record, or would it be more reasonable to create a custom view to capture the value and pass the request directly to the general view from my overridden view?

+3
source share
1 answer

I am doing some redirects in urls.py as follows, maybe this works for you too?

from django.views.generic.base import RedirectView
urlpatterns = patterns('',
    (r'^manual/glossary/(?P<slug>[^/]+)/$',
        RedirectView.as_view(url='/glossary/%(slug)s/')),
)

, , :

from django.views.generic.list import ListView
urlpatterns = patterns('',
    (r'^tag/(?P<tag>\d+)/$',
        ListView.as_view(
            queryset=Blog.Post.objects.filter(tags='%(tag)d'),
            paginate_by=5)),
)

snipplet , ListView , .

+2

Source: https://habr.com/ru/post/1742216/


All Articles