Package: django-url-tracker-forl
Thedjango-url-trackeris meant to be a easy-to-use addition to
a website to enhance its SEO. This might seem slightly pointless
asCool URIs don’t change. I don’t want to argue
with that and not changing URL should be the primary goal. In case,
a URL is changed for some reason, however, this can reflect badly in
terms of SEO as search engines do not appreciate ending up on a 404
page when crawling a known URL. To handle these situations nicelydjango-url-trackerkeeps track of URL changes and when theoldURL is called provides a permanent redirect (HTTP 301) or a gone
response (HTTP 410) for deleted URLs.The tracking is aimed at those URLs that are generated based on
model fields, e.g. aslugfield. To start tracking URL changes
for a particular model, you simply have to register the model
withurl_trackerand everytime a model is changed or deleted,
URL changes are recorded.The HTTP repsonses that provide anHttpResponsePremanentRedirectorHttpResponseGoneare handled similar to theflatpagesmiddleware, intercepting404exceptions and checking for the
requested URLs in all existingURLChangeRecords. Depending
on the recorded data the corresponding HTTP response is return or
a404is raised when no URL matching the requested one can be
found.InstallationInstallation is as easy as:pip install django-url-tracker-forkDone!ConfigurationTo start usingurl_trackerin your project. Just add the
following two lines to yoursettings.py:Add the middlewareurl_tracker.middleware.URLChangePermanentRedirectMiddlewareto the end ofMIDDLEWARE_CLASSESwhich should look similar
to this afterwards:MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
    'url_tracker.middleware.URLChangePermanentRedirectMiddleware',
)Addurl_trackeranddjango.contrib.contenttypes, included by
default, to yourINSTALLED_APPS:INSTALLED_APPS = (
    "django.contrib.contenttypes",
    "url_tracker",
)Now you are able to useurl_trackerwithin you project. All that
remains to do is registering a model for tracking.Tracking A ModelThis is just a simple example of how to track a model. Let’s assume we
have a modelProjectthat hold details on this particular project and
is made available at the URLhttp://www.example.com/project/some-project/.
The project’s URL is based on theSlugFieldof our model. The model could
look like this:class Project(models.Model):
    name = models.CharField(max_length=20)
    slug = models.SlugField(max_length=20)
    description = models.CharField(max_length=500)I will not go into details of how to create the slug as I think this is
common practise. So for now we just assume thatslugis populated
automatically fromname. One other thing, however, is required for
the tracker to work, theget_absolute_urlmethod. Let’s add this to
the model:class Project(models.Model):
    ...

    @models.permalink
    def get_absolute_url(self):
        return ('project-detail', (), {'slug': self.slug})And now the missing link to actually start tracking URL changes. Add theURLTrackingMixinto your model and add the following command to the
bottom of the class definition (or the file if you prefer that):import url_tracker

class Project(url_tracker.URLTrackingMixin, models.Model):
    ...

url_tracker.track_url_changes_for_model(Project)To change which functions are called to tracks urls, add aurl_tracking_methodsattribute to the clast, which is a list of
method names to track. By default the the list containsget_absolute_url.You are done. If you go to the admin interface, create a new project
and then change its slug (which changes its URL) you will see a newURLChangeRecordreflecting the change. Opening theold_urlshould
then redirect you to thenew_url.ContributingIf you find issues or would like to see a feature suppored, head over to
theissues section:and report it.To contribute code in any form, fork thegithub repository:and clone it locally.
Create a new branch for your feature:git commit -b feature/whatever-you-likeThen make sure all the tests past (and write new ones for any new features):./run_tests.pypush the finished feature to github and open a pull request form the branch.If you make a change to models.py that requires a database migration,
usedjango-mini.py-p-aurl_tracker-asouth-adjango.contrib.contenttypes schemamigration url_tracker--autoto create a south migration.