1

我正在使用 Django-MPTT 为我的 Django 项目开发分类应用程序。但我似乎无法获得添加斜杠的正则表达式模式,该斜杠也不会中断子类别。

这是一个示例 URL: 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)
4

3 回答 3

1

这应该匹配不带斜杠的 url:

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

您可以将此匹配重定向到 full_slug + "/"。

于 2011-11-27T08:02:52.633 回答
1

原来我的模式有点偏离。URLConf 中的正确模式是(r'^(?P<full_slug>[-\w/]+/)$'

于 2011-11-27T11:43:29.753 回答
0

我最终选择了这个解决方案:url(r'^(?P<full_slug>[-\w/]+)'...

于 2013-11-25T16:27:40.160 回答