Fixed the get_absolute_url for the Categories model and fixed up the view as well

This commit is contained in:
Corey Oordt 2009-10-06 09:28:16 -04:00
parent 3ea529c71b
commit e1da454bee
4 changed files with 15 additions and 6 deletions

View file

@ -1,5 +1,6 @@
import re
from django.core.urlresolvers import reverse
from django.db.models import permalink
from django.db import models
from django.utils.encoding import force_unicode
import mptt
@ -16,11 +17,12 @@ class Category(models.Model):
order = models.IntegerField(blank=True, null=True)
slug = models.SlugField()
@permalink
def get_absolute_url(self):
"""Return a path"""
prefix = self.category_tree.get_absolute_url()
ancestors = self.get_ancestors()
return prefix + '/'.join([force_unicode(i.slug) for i in ancestors])
prefix = reverse('categories_tree_list')
ancestors = list(self.get_ancestors()) + [self,]
return prefix + '/'.join([force_unicode(i.slug) for i in ancestors]) + '/'
class Meta:
verbose_name_plural = 'categories'

View file

@ -12,5 +12,5 @@ urlpatterns = patterns('django.views.generic.list_detail',
)
urlpatterns += patterns('categories.views',
url(r'^(?P<slug>[\w-]+)/$', 'category_detail', {'with_stories': True}, name='categories_category'),
url(r'^(?P<path>.+)$', 'category_detail', {'with_stories': True}, name='categories_category'),
)

View file

@ -2,12 +2,17 @@ from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext
from categories.models import Category
from django.views.decorators.cache import cache_page
from django.core.urlresolvers import reverse
def category_detail(request, slug, with_stories=False,
def category_detail(request, path, with_stories=False,
template_name='categories/category_detail.html'):
path_items = path.strip('/').split('/')
slug = path_items[-1]
level = len(path_items)
context = {}
category = get_object_or_404(Category,
slug__iexact=slug)
slug__iexact=slug, level=level-1)
context['category'] = category

View file

@ -19,6 +19,8 @@ urlpatterns = patterns('',
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
(r'^categories/', include('categories.urls')),
#(r'^cats/', include('categories.urls')),
(r'^static/categories/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': ROOT_PATH + '/categories/media/categories/'}),