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
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 raises a KeyDoesNotExist exception if a particular URL cannot be found. This can be prevented by passing fail_silently=True.
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.
Release files for django-keyed-urls 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-keyed-urls-0.4.1.tar.gz | 7.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_keyed_urls-0.4.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 17.7 kB
Release files / django-keyed-urls-0.4.1.tar.gz
| Download URL | django-keyed-urls-0.4.1.tar.gz |
|---|---|
| Size | 7.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e33f6e474c20d43ee4807d09fdc0e8ded462c5589fb75024bb99d179c7b5de20
|
|
BLAKE2b-256 checksum How to use checksums |
58723a8ec0f7a5a1cae7263b7c5b648d270df29d2a577be4cf9c631e54e9117d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_keyed_urls-0.4.1-py2.py3-none-any.whl
| Download URL | django_keyed_urls-0.4.1-py2.py3-none-any.whl |
|---|---|
| Size | 10.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
97d942801db63aa0852e3a9b5a4c77e00a7f31eff30bc05f5ec12abfda0ecaa5
|
|
BLAKE2b-256 checksum How to use checksums |
808cb7da8f13143b6ae69ec62103bb65a5695441e9cfb43993ed4f7ff7825395
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |