python - Django - Multiple <pk> in urls.py with DetailViews -


i'm trying build script using django generic display views.

my urls.py

url(r'^page/(?p<pk>[0-9]+)/$', pagedetails.as_view(), name='page-details'), 

my views.py

class pagedetails(detailview):     model = pages      def get_context_data(self, **kwargs):         context = super(pagedetails, self).get_context_data(**kwargs)         return context 

the problem how can set multi <pk> in urls this?

url(r'^page/(?p<pk>[0-9]+)/subpage/(?p<pk>[0-9]+)$', pagedetails.as_view(), name='page-details'), 

in views need take data first , second <pk>.

change second pk argument in url else, example pk_alt:

^page/(?p<pk>[0-9]+)/subpage/(?p<pk_alt>[0-9]+)$ 

the url parameter pk_alt available in views function part of self.kwargs dictionary, can access using:

self.kwargs.get('pk_alt', '') 

you update views following:

class pagedetails(detailview):     model = pages      def get_context_data(self, **kwargs):         context = super(pagedetails, self).get_context_data(**kwargs)         page_alt = pages.objects.get(id=self.kwargs.get('pk_alt', ''))         context['page_alt'] = page_alt         return context 

you able access alternative model in template using {{ page_alt }}


Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -