Code Monkey home page Code Monkey logo

django-shorturls's Introduction

django-shorturls
================

A custom URL shortening app for Django, including easy ``rev=canonical``
support.

Most code was originally by Simon Willison; see
http://simonwillison.net/2009/Apr/11/revcanonical/ for details. Improved
slightly and packaged by Jacob Kaplan-Moss.

Patches welcome: http://github.com/jacobian/django-shorturls

Usage
=====

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 shorturl %}
        <a href="{% shorturl object %}">...</a>
        
   (where ``object`` is a model instance).
  
   Alternatively::
  
        {% load shorturl %}
        {% revcanonical 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.

django-shorturls's People

Contributors

jacobian avatar d0ugal avatar camflan avatar matthewryanscott avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.