How do I add a trailing slash for Django MPTT-base

2019-06-03 13:57发布

问题:

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

And here is my 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']

And finally, my model:

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)

回答1:

This should match urls without trailing slash:

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

You can redirect this matches to full_slug + "/".



回答2:

Turns out my pattern was off slightly. The correct pattern in the URLConf is (r'^(?P<full_slug>[-\w/]+/)$'



回答3:

I ended up going with this solution: url(r'^(?P<full_slug>[-\w/]+)'...