......................................
........../\............./\...........
........./..\.........../..\..........
.......././\.\__-----__/./\.\.........
......./.....................\........
......|.....(.o.).__.(.o.)....|.......
.....~~~~~~...../.ºº.\.....~~~~~~.....
...~~~~~~....(.../--\...)....~~~~~~...
........\\.......PURR.......//........
............\\\/\/||\/\///............
..................\/..................
......................................
The Category
model was taken from Satchmo Project, I've only added slight modifications.
Note: Tested using Django (1, 3, 0, 'final', 0)
.
Django categories, sub-categories and sub-sub-categories
Using PIP.
The repository will be checked out in a temporary folder, installed, and cleaned up:
$ pip install git+https://github.com/mhulse/django-purr.git
This can be combined with the -e
flag, and Pip will perform the checkout in ./src/
. You need to supply a name for the checkout folder by appending a hash to the repository URL:
$ pip install -e git+https://github.com/mhulse/django-purr.git#egg=django-purr
More info here.
Next, add purr
to your INSTALLED_APP
in your app's settings.py
:
INSTALLED_APPS = (
# ...
'purr',
# ...
)
Finally, add this to your app's urls.py
tuple:
urlpatterns = patterns('',
# ...
(r'^purr/', include('purr.urls')),
# ...
)
Upgrade using:
$ pip install --upgrade git+https://github.com/mhulse/django-purr.git
… and migrate your schema accordingly.
Uninstall using:
$ pip uninstall django-purr
Given this URI:
http://site.com/purr/business/delivery-only/italian/pizza/
… the output will simply be:
{
"category": "Pizza"
}
The code itterates over each sequential category slug
and and will throw a Page not found (404)
if the category structure doesn't exist; for example:
http://testprojects.registerguard.com/purr/business/delivery-only/ddd/pizza/
… won't work because category slug ddd
doesn't exist as a child to /business/delivery-only/
.
There's a tester script, found in the scripts
folder, that you can run like so:
$ python manage.py runscript purr_tests
… the output will be (it will help if you've installed the initial_data.json
fixture):
[<Category: Business>,
<Category: BUSINESS | Sports>,
<Category: BUSINESS | SPORTS | Eating contest>,
<Category: BUSINESS | SPORTS | EATING CONTEST | Pizza>]
Copyright © 2012 Micky Hulse
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.