2011-11-27 65 views
1

我正在使用Django-MPTT爲我的Django項目開發一個分類應用程序。但我似乎無法得到正則表達式模式來添加不會在子類別上打破的尾部斜槓。如何爲基於Django MPTT的分類應用程序添加尾部斜線?

下面是一個示例網址:http://mydjangoapp.com/categories/parentcat/childcat/我希望能夠使用http://mydjangoapp.com/categories/parentcat並將其重定向到尾部斜槓版本。這同樣適用於http://mydjangoapp.com/categories/parentcat/childcat(它應該重定向到http://mydjangoapp.com/categories/parentcat/childcat/)。

這裏是我的urls.py:

from django.conf.urls.defaults import patterns, include, url 
from django.views.decorators.cache import cache_page 

from storefront.categories.models import Category 
from storefront.categories.views import SimpleCategoryView 

urlpatterns = patterns('', 
    url(r'^(?P<full_slug>[-\w/]+)', cache_page(SimpleCategoryView.as_view(), 60 * 15), name='category_view'), 
) 

這是我的觀點:

from django.core.exceptions import ImproperlyConfigured 
from django.core.urlresolvers import reverse 
from django.views.generic import TemplateView, DetailView 
from django.views.generic.detail import SingleObjectTemplateResponseMixin, SingleObjectMixin 
from django.utils.translation import ugettext as _ 
from django.contrib.syndication.views import Feed 

from storefront.categories.models import Category 

class SimpleCategoryView(TemplateView): 

    def get_category(self): 
     return Category.objects.get(full_slug=self.kwargs['full_slug']) 

    def get_context_data(self, **kwargs): 
     context = super(SimpleCategoryView, self).get_context_data(**kwargs) 
     context["category"] = self.get_category() 
     return context 

    def get_template_names(self): 
     if self.get_category().template_name: 
      return [self.get_category().template_name] 
     else: 
      return ['categories/category_detail.html'] 

最後,我的模型:

from django.db import models 
from mptt.models import MPTTModel 
from mptt.fields import TreeForeignKey 

class CategoryManager(models.Manager): 
    def get(self, **kwargs): 
     defaults = {} 
    defaults.update(kwargs) 
     if 'full_slug' in defaults: 
      if defaults['full_slug'] and defaults['full_slug'][-1] != "/": 
       defaults['full_slug'] += "/" 
     return super(CategoryManager, self).get(**defaults) 

class Category(MPTTModel): 
    title = models.CharField(max_length=255) 
    description = models.TextField(blank=True, help_text='Please use <a href="http://daringfireball.net/projects/markdown/syntax">Markdown syntax</a> for all text-formatting and links. No HTML is allowed.') 
    slug = models.SlugField(help_text='Prepopulates from title field.') 
    full_slug = models.CharField(max_length=255, blank=True) 
    template_name = models.CharField(max_length=70, blank=True, help_text="Example: 'categories/category_parent.html'. If this isn't provided, the system will use 'categories/category_detail.html'. Use 'categories/category_parent.html' for all parent categories and 'categories/category_child.html' for all child categories.") 

    parent = TreeForeignKey('self', null=True, blank=True, related_name='children') 

    objects = CategoryManager() 

    class Meta: 
     verbose_name = 'category' 
     verbose_name_plural = 'categories' 

    def save(self, *args, **kwargs): 
     orig_full_slug = self.full_slug 
     if self.parent: 
      self.full_slug = "%s%s/" % (self.parent.full_slug, self.slug) 
     else: 
      self.full_slug = "%s/" % self.slug 
     obj = super(Category, self).save(*args, **kwargs) 
     if orig_full_slug != self.full_slug: 
      for child in self.get_children(): 
       child.save() 
     return obj 

    def available_product_set(self): 
     """ Returns available, prioritized products for a category """ 
     from storefront.apparel.models import Product 
     return self.product_set.filter(is_available=True).order_by('-priority') 

    def __unicode__(self): 
     return "%s (%s)" % (self.title, self.full_slug) 

    def get_absolute_url(self): 
     return '/categories/%s' % (self.full_slug) 

回答

0

我結束了這個解決方案會:url(r'^(?P<full_slug>[-\w/]+)'...

1

這應該與網址,沒有結尾的斜線:

r'^(?P<full_slug>[-\w/]+)/$' 

您可以將此匹配重定向到full_slug +「/」。

+0

這確實匹配,但是網址上孩子類別打破。換句話說,這是有效的:http://mydjangoapp.com/categories/parentcat/但這不:http://mydjangoapp.com/categories/parentcat/childcat/ –

1

原來我的模式稍微偏了。 URL配置正確的模式是(r'^(?P<full_slug>[-\w/]+/)$'