Skip to main content

So simple you'll burst into tears right away.

Project description

https://travis-ci.org/matthiask/django-keyed-urls.png?branch=master

An app for those cases when you need language-specific URLs in the database for use in templates or as redirects.

Installation

Install django-keyed-urls with pip:

pip install django-keyed-urls

Add keyed_urls and modeltranslation to INSTALLED_APPS.

Specify a custom location for South migrations for keyed_urls – you really want to use South to handle schema changes when adding or removing languages from LANGUAGES:

SOUTH_MIGRATION_MODULES = {
    'keyed_urls': 'yourapp.migrate.keyed_urls',
}

Create and run database migrations:

python manage.py schemamigration keyed_urls --initial
python manage.py migrate keyed_urls

If you want to use the forwarding URLs, add an entry to your URLconf file. It is recommended to use i18n_patterns because that means that language-specific redirection URLs are automatically handled correctly:

from django.conf.urls import include, url
from django.conf.urls.i18n import i18n_patterns

urlpatterns += i18n_patterns(
    '',
    url(r'', include('keyed_urls.urls'),
)

Usage

Template tags

Usage is simple:

{% load keyed_urls %}

{% keyed_url 'some_key' as url %}

<a href="{{ url }}">bla</a>

<a href="{% keyed_url 'some_other_key' %}">bla</a>

Or:

{% load keyed_urls %}

{% keyed_url 'some_key' language='en' as url %}

<a href="{{ url }}">bla</a>

If a cartain key does not exist, the tag currently returns (or assigns) None. This behavior makes sense when using keyed_url as an assignment tag, but is not very helpful otherwise.

Helpers

Two additional helpers are available. If you need URLs in python code, use the following snippet:

from keyed_urls import get_url

some_url = get_url('some_key')
some_other_url = get_url('some_key', language='en')

The advantage of using get_url compared to fetching a KeyedURL model from the database and accessing its url attribute is that get_url is caching all results. Since get_url is also used internally by the template tag described above this means that you do not have to worry about performance as much as when using models directly. get_url returns None if a particular URL cannot be found.

The following snippet can be used to fetch the forwarding URL:

from keyed_urls import get_forwarding_url

url = get_forwarding_url('some_key')
url = get_forwarding_url('some_key', language='de')

get_forwarding_url is nothing more but a thin wrapper around Django’s own reverse method. This method raises a NoReverseMatch exception if the key is invalid, but does not check whether the given key exists at all in the database. When visiting the link, users will get a 404 response. For get_forwarding_url to work you have to include keyed_urls.urls somewhere in your URLconf as described above, preferrably inside an i18n_patterns block.

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-keyed-urls-0.3.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

django_keyed_urls-0.3.0-py2.py3-none-any.whl (9.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-keyed-urls-0.3.0.tar.gz.

File metadata

File hashes

Hashes for django-keyed-urls-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1aa4d8f755cbe51b44811595e1349041627595e42911c16c28262d4c3b6899d7
MD5 3ee7bccb342043053ab23bcf6642d1e4
BLAKE2b-256 56316774447b5c3b71d9dae44f893cda7d44317e9b100ca02bc642a24cba0656

See more details on using hashes here.

File details

Details for the file django_keyed_urls-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_keyed_urls-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 40ea747e223844bb1f59a4cec9c849656f2a9366456f203fbc5225f96215c626
MD5 98ab078fdd7473e52dc22410a98d8282
BLAKE2b-256 2cf6dfc254dd9d88d4f811216ec9f2d0a618af5460d38dcfdd3d6d04ac7b355e

See more details on using hashes here.

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