1
votes

Im trying to implement a gallery app in django cms.

I want urls like this: www.site-url.com/gallery/category/gallery-name

My solution is not fully working. If I go to this url http://127.0.0.1:8000/en/gallery/1001/test/ where "1001" is a category and "test" is gallery-name, I want to check if a category exist and if a gallery belongs to this category.

Next problem is, if I want to get full url of gallery with get_absolute_url() method. My urls.py are bad implemented, so it's not working.

my urls.py:

from django.conf.urls import patterns, url
from .views import GalleryListView, GalleryDetailView

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

my models.py:

class Category(Sortable):
    parent = models.ForeignKey('self', blank=True, null=True)

    name = models.CharField()
    slug = AutoSlugField(populate_from='name')

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return "#"

class Gallery(Sortable):
    name = models.CharField()

    parent = models.ForeignKey(Category, blank=False, null=True)

    slug = AutoSlugField(populate_from='name')

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('gallery_detail', args=[self.pk])
2

2 Answers

2
votes

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'])
0
votes

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'])