Skip to main content

Django app for creating hierarchical URLs associated with django-MPTT models.

Project description

Overview
--------

This reusable app helps to create arbitrary-depth [Clean URLs](http://en.wikipedia.org/wiki/Clean_URL) which 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.

Example
-------

The 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 runserver
```

And visit 127.0.0.1:8000 in your browser.

Requirements
------------

Developed using django 1.7, python 3.4.3, other versions may work too. If python 2 support required, just fork and patch, there are only few lines of code there.

django-mptt-urls uses [django-mptt](https://github.com/django-mptt/django-mptt). It will be automatically installed as a requirement.

Installation
------------

First 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-urls
```

Then, in your `urls.py`, replace one or more views with special `mptt_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'), {'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 to `mptt_urls.view` when url resolution is fired. You can define `path` whatever you like (for example `(?P<path>[\d/]+)`), but **don't forget to include '/' in the regex**.
* Replace your view with special `mptt_urls.view`.

`mptt-urls.view` works like a decorator to a view: it gets fired when url resolution is performed, calculates an instance the `path` is poining to, and passes it to original view.

So, if you write
```
url(r'^gallery/(?P<path>.*)', 'gallery.views.category', name='gallery'),
```
the `gallery.views.category` view will receive `path` variable and will have to make object resolution.
With `mptt_urls.view`, you will get the resolved object automatically - `gallery.views.category` view will receive `path` and `instance` variables:
```
url(r'^gallery/(?P<path>.*)', mptt_urls.view(model='gallery.models.Category', view='gallery.views.category', slug_field='slug'), name='gallery'),
```

`mptt_urls.view` options are:
* `model` - your model derived from MPTTModel.
* `view` - a view which will process the request. Don't forget to capture `path` and `instance` (`def category(request, path, instance):`). If resolution fails, `instance` will be None.
* `slug_field` - the field where model instance's slug is stored

get_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 use `Category.get_path()` which is available since using `mptt_urls.view`.

License
-------
MIT. Do whatever you like.
View license file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-mptt-urls-2.0.0a1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

django_mptt_urls-2.0.0a1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file django-mptt-urls-2.0.0a1.tar.gz.

File metadata

File hashes

Hashes for django-mptt-urls-2.0.0a1.tar.gz
Algorithm Hash digest
SHA256 e48f2625feaa9da78c4c072d055671dc903ea3ef2d21f98eaca727a028a8b5c5
MD5 64c1522a70e4970a66e0a7a5b453ff34
BLAKE2b-256 aaa190cf52bb181623b56649eb4c9e8b6e6f01af4457f46a20b15c76ac03158b

See more details on using hashes here.

File details

Details for the file django_mptt_urls-2.0.0a1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_mptt_urls-2.0.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 ce4982e2ea8f285c90b959480269ef57c53c3811dde0f3f134c4510b6b14f2cb
MD5 482b49c166c9f6f0521705ff83faa43a
BLAKE2b-256 34b0915e76332afa82886fa86686ed70e3dfbfe18127b8c54549a29b1687d6f2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page