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 hashes)

Uploaded Source

Supported by

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