[Answered ]-Django-CMS apphooks menu and reverse

0đź‘Ť

âś…

Finally I found a solution for get_absolute_url() method in this youtube tutorial https://www.youtube.com/watch?v=Dj8dhgmzlFM

I modified get_absolute_url() in models.py like this:

def get_absolute_url(self):
   return reverse('gallery:gallery_detail', kwargs={'slug': self.slug, 'parent_slug': self.parent.slug})

Where “gallery” is app_name in cms_apps.py:

from cms.app_base import CMSApp
from cms.apphook_pool import apphook_pool
from django.utils.translation import ugettext_lazy as _
from .cms_menus import GalleryMenu


class GalleryApp(CMSApp):
    name = _('Gallery')
    urls = ['app.apps.gallery.urls', ]
    app_name = 'gallery'
    menus = [GalleryMenu]

apphook_pool.register(GalleryApp)

Then I want to check if a category exist and if a gallery belongs to this category.

According to Alasdair answer:

To fetch the correct object in your GalleryDetailView, you need to
override the get_object method. You can access the slugs from
self.kwargs.

GalleryDetailView(DetailView):
    ...
    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()
        return queryset.get(parent__slug=self.kwargs['parent_slug'], slug=self.kwargs['slug'])
👤vegazz

2đź‘Ť

You want your get_absolute_url method to match this url pattern,

url(r'^(?P<parent_slug>[-\w]+)/(?P<slug>[-\w]+)/$', GalleryDetailView.as_view(), name="gallery_detail"),

so you need to provide two arguments, the parent slug and the gallery’s slug:

class Gallery(Sortable):
    def get_absolute_url(self):
        return reverse('gallery_detail', args=[self.parent.slug, self.slug])

To fetch the correct object in your GalleryDetailView, you need to override the get_object method. You can access the slugs from self.kwargs.

GalleryDetailView(DetailView):
    ...
    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()
        return queryset.get(parent__slug=self.kwargs['parent_slug'], slug=self.kwargs['slug'])
👤Alasdair

Leave a comment