Package: django-mptt-urls
OverviewThis reusable app helps to create arbitrary-depthClean URLswhich correspond to MPTT tree hierarchy of model instances, like these:http://best-photographer.com/gallery/weddings/Dexter-and-Rita/http://best-photographer.com/gallery/my-pets/dogs/husky/As you can see, the links are quite different - they have different depth of hierarchy. When users see these URLs they can easily discover where they are located. They can either delete some part of the URL and thus move up in the hierarchy.Django-mptt-urls is just a simple view that knows how to resolve hierarchical urls.ExampleThe simpliest way to understand how django_mptt_urls works is to clone this GitHub project, create virtual environment and run test_project (no extra settings required, except sqlite3 database support):git clone https://github.com/c0ntribut0r/django-mptt-urls.git
pyvenv django-mptt-urls
source django-mptt-urls/bin/activate
pip install django-mptt-urls/
python django-mptt-urls/test_project/manage.py runserverAnd visit 127.0.0.1:8000 in your browser.Requirementsdjango-mptt-urls usesdjango-mptt. It will be automatically installed as a requirement.InstallationFirst of all, you should already be using django-mptt, something like this:from mptt.models import MPTTModel, TreeForeignKey

class Category(MPTTModel):
    ...
    parent = TreeForeignKey('self', null=True, blank=True)
    slug = models.SlugField()
    class Meta:
        unique_together = ('slug', 'parent')Install django-mptt-urls:pip install django-mptt-urlsThen, in yoururls.py, replace one or more views with specialmptt_urls.view:# urls.py
...
import mptt_urls

urlpatterns = patterns('',
    ...
    url(r'^gallery/(?P<path>.*)', mptt_urls.view(model='gallery.models.Category', view='gallery.views.category', slug_field='slug', trailing_slash=True), {'extra': 'You may also pass extra options as usual!'}, name='gallery'),
    ...
)Here is what we've done:We are capturing(?P<path>.*), which will be passed tomptt_urls.viewwhen url resolution is fired. You can definepathwhatever you like (for example(?P<path>[\d/]+)), butdon't forget to include '/' in the regex.Replace your view with specialmptt_urls.view.mptt-urls.viewworks like a decorator to a view: it gets fired when url resolution is performed, calculates an instance thepathis poining to, and passes it to original view.So, if you writeurl(r'^gallery/(?P<path>.*)', 'gallery.views.category', name='gallery'),thegallery.views.categoryview will receivepathvariable and will have to make object resolution.
Withmptt_urls.view, you will get the resolved object automatically -gallery.views.categoryview will receivepathandinstancevariables:url(r'^gallery/(?P<path>.*)', mptt_urls.view(model='gallery.models.Category', view='gallery.views.category', slug_field='slug'), name='gallery'),mptt_urls.viewoptions are:model- your model derived from MPTTModel.view- a view which will process the request. Don't forget to capturepathandinstance(def category(request, path, instance):). If resolution fails,instancewill be None.slug_field- the field where model instance's slug is storedget_absolute_url()Well, url(...) defines direct url resolution.
To define reverse url resolution, add to your model:class Category(MPTTModel):
    ...
    def get_absolute_url(self):
        return reverse('gallery', kwargs={'path': self.get_path()})Here, we useCategory.get_path()which is available since usingmptt_urls.view.If you use namespaced url routing, don't forget to addnamespace specifierlike this:return reverse('namespace:gallery', kwargs={'path': self.get_path()})LicenseMIT. Do whatever you like.
View license file for details.