diff options
| author | Jacob Kaplan-Moss | 2009-04-12 18:45:51 -0500 |
|---|---|---|
| committer | Jacob Kaplan-Moss | 2009-04-12 18:45:51 -0500 |
| commit | 1210c461e575e14b4b8eb90f429757456cbea8e0 (patch) | |
| tree | 36675e533662014ca6d7b61b86e1fe5e00719349 | |
| parent | 9d86549d74c6d3f4d7e661fd643aee739e268a6d (diff) | |
| download | django-shorturls-1210c461e575e14b4b8eb90f429757456cbea8e0.tar.bz2 | |
Added docs, and fixed a bug that writing the docs revealed.
| -rw-r--r-- | README | 68 | ||||
| -rw-r--r-- | src/shorturls/templatetags/shorturl.py | 16 |
2 files changed, 75 insertions, 9 deletions
@@ -11,5 +11,71 @@ slightly and packaged by Jacob Kaplan-Moss. Usage ===== -XXX +So, you want to host your own short URLs on your Django site: + 1. In your settings, define a set of prefixes for short URLs:: + + SHORTEN_MODELS = { + 'A': 'myapp.animal', + 'V': 'myapp.vegetable', + 'M': 'myapp.mineral' + } + + The keys are string prefixes; they can be any string, actually, + but since we're going for short a single character is probably good. + + Values are the (hopefully-familiar) ``"<app-name>.<model-class>"`` used + by Django to identify a model. Remember: ``app-name`` is the + (case-sensitive) last bit of your app's name in ``INSTALLED_APPS``, and + ``<model-class>`` is your model class's name, lowercased. + + Make sure your models have a ``get_absolute_url()`` method defined. + + 2. Wire up the redirect view by adding to your URLconf:: + + ('^short/', include('shorturls.urls')) + + 3. If you'd like to quickly link to shortened URLs in your templates, stick + ``"shorturls"`` in ``INSTALLED_APPS``, and then in your templates do:: + + {% load shorturls %} + <a href="{% shorturl object %}">...</a> + + (where ``object`` is a model instance). + + Alternatively:: + + {% load shorturls %} + {% recanonical object %} + + This generates the whole ``<link rev="canonical" href="...">`` tag for + you. + +That's it. + +If you'd like more control, keep reading. + +Settings +======== + +Available settings are: + + ``SHORTEN_MODELS`` + You've seen this one. + + ``SHORT_BASE_URL`` + If defined, the ``shorturl`` and ``revcanonical`` template tags will + prefix generated URLs with this value. Use this if you've got a shorter + domain name you'd like to use for small URLs. + + For example, given ``SHORT_BASE_URL = 'http://exm.pl/'``, ``{% shorturl + obj %}`` would return something like ``http://exm.pl/AbCd``. + + ``SHORTEN_FULL_BASE_URL`` + The domain to redirect to when redirecting away from the small URL. + Again, you'll use this if your short URL base and your "real" site + differ. + + If not defined, the redirect view will try to guess the proper domain by + consulting the ``django.contrib.sites`` framework, if installed, or the + requested domain, if not.
\ No newline at end of file diff --git a/src/shorturls/templatetags/shorturl.py b/src/shorturls/templatetags/shorturl.py index 5d4ec5e..2eef0ce 100644 --- a/src/shorturls/templatetags/shorturl.py +++ b/src/shorturls/templatetags/shorturl.py @@ -26,19 +26,19 @@ class ShortURL(template.Node): prefix = self.get_prefix(obj) except (AttributeError, KeyError): return '' - + + tinyid = base62.from_decimal(obj.pk) + + if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL: + return urlparse.urljoin(settings.SHORT_BASE_URL, prefix+) + try: - url = urlresolvers.reverse('shorturls.views.redirect', kwargs = { + return urlresolvers.reverse('shorturls.views.redirect', kwargs = { 'prefix': prefix, - 'tiny': base62.from_decimal(obj.pk) + 'tiny': tinyid }) except urlresolvers.NoReverseMatch: return '' - - if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL: - return urlparse.urljoin(settings.SHORT_BASE_URL, url) - else: - return url def get_prefix(self, model): if not hasattr(self.__class__, '_prefixmap'): |
