Skip to main content

Use friendly urls with Django.

Project description

This Django application allows you to create urls that directly return views.
There is no 302 Redirect.

The most common use case is that you have URLs which return user profiles such as

http://www.example.com/user/1

Friendly URLs allow you to create a URL so that the user can also be accessed by going to:

http://www.example.com/jeff

The key is that the address bar in the browser does not change to http://www.example.com/user/1
The web server returns the same HTML document as http://www.example.com/user/1

Flow
-----

The application looks up the string 'jeff' If it finds a friendly url match it will read out the absolute_path
and attempt to resolve it by putting it through the URL resolver a 2nd time.

If it finds a resolution it will return that view.


Usage
-----

Here is an example of how to implement friendly_urls for a user:

I put this in models.py:

from django.contrib.contenttypes import generic
from friendlyurls import UrlMapping
from django.contrib.auth.models import User

def get_absolute_url(self):
return u'/user/%s' % self.id

User.add_to_class('friendly_urls', generic.GenericRelation(UrlMapping))
User.get_absolute_url = get_absolute_url

This basically monkey patches the User model to add a field. You can then do

user = User.objects.get(pk=1)
user.friendly_urls.all()

This will get a list of all the friendly urls.

You shouldn't have to monkey patch all the models.

Problems / Questions
--------------------

Don't forget to run syncdb because friendlyurls creates a table to store the URL mappings.


Additional Settings
-------------------

FRIENDLYURLS_IGNORE_CASE - Set this to True if you want to ignore the case when trying to find a path. Default: False

Todo Features
-------------

- Need to cache the results returned by a lookup for performance.
Probably do a from django.core.cache import cache

https://docs.djangoproject.com/en/dev/topics/cache/?from=olddocs

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_friendlyurls-0.1.1.tar.gz (4.7 kB view details)

Uploaded Source

File details

Details for the file django_friendlyurls-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django_friendlyurls-0.1.1.tar.gz
Algorithm Hash digest
SHA256 387c506b7a0ec4aa16b9088158b4b513cda95f67438bd08112aadc98c04dfff8
MD5 15417c42b71436a09261eaea9e61feb8
BLAKE2b-256 911369d85892b2a6c8f5a09a338b88b1da451a4fe949d8d664dba6956a5d2339

See more details on using hashes here.

Supported by

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