这是我的urls.py:from django.conf.urls.defaults import patterns, include, urlfrom django.views.decorators.cache import cache_pagefrom storefront.categories.models import Categoryfrom storefront.categories.views import SimpleCategoryViewurlpatterns = patterns('', url(r'^(?P<full_slug>[-\w/]+)', cache_page(SimpleCategoryView.as_view(), 60 * 15), name='category_view'),)这是我的观点:from django.core.exceptions import ImproperlyConfiguredfrom django.core.urlresolvers import reversefrom django.views.generic import TemplateView, DetailViewfrom django.views.generic.detail import SingleObjectTemplateResponseMixin, SingleObjectMixinfrom django.utils.translation import ugettext as _from django.contrib.syndication.views import Feedfrom storefront.categories.models import Categoryclass 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 modelsfrom mptt.models import MPTTModelfrom mptt.fields import TreeForeignKeyclass 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)解决方案我最终采用了以下解决方案:url(r'^(?P<full_slug>[-\w/]+)'... I'm using Django-MPTT to develop a categorization app for my Django project. But I can't seem to get the regex pattern for adding a trailing slash that doesn't also break on child categories.Here's an example URL: http://mydjangoapp.com/categories/parentcat/childcat/ I'd like to be able to use http://mydjangoapp.com/categories/parentcat and have it redirect to the trailing slash version. The same should apply to http://mydjangoapp.com/categories/parentcat/childcat (it should redirect to http://mydjangoapp.com/categories/parentcat/childcat/).Here's my urls.py:from django.conf.urls.defaults import patterns, include, urlfrom django.views.decorators.cache import cache_pagefrom storefront.categories.models import Categoryfrom storefront.categories.views import SimpleCategoryViewurlpatterns = patterns('', url(r'^(?P<full_slug>[-\w/]+)', cache_page(SimpleCategoryView.as_view(), 60 * 15), name='category_view'),)And here is my view:from django.core.exceptions import ImproperlyConfiguredfrom django.core.urlresolvers import reversefrom django.views.generic import TemplateView, DetailViewfrom django.views.generic.detail import SingleObjectTemplateResponseMixin, SingleObjectMixinfrom django.utils.translation import ugettext as _from django.contrib.syndication.views import Feedfrom storefront.categories.models import Categoryclass 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']And finally, my model:from django.db import modelsfrom mptt.models import MPTTModelfrom mptt.fields import TreeForeignKeyclass 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) 解决方案 I ended up going with this solution: url(r'^(?P<full_slug>[-\w/]+)'... 这篇关于如何为基于Django MPTT的分类应用添加结尾斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 07:58