Skip to main content

Use connection URLs for Database/Cache settings in your Django project.

Project description

This is a fork of Kenneth Reitz’s DJ-Database-URL extended to support cache URLs as well.

There are some significant changes to the original, most notably, .parse() has been dropped. You can pass URLs and environment variables to .config(). The passed value will be first checked if it is an environment variable, and if not found it will be parsed as a URL. .config() will not use DATABASE_URL as default and therefore cannot be called without parameters.

The connection_url.config method returns a Django database/cache connection dictionary, populated with all the data specified in your URL. Multiple dictionary objects can be passed to .config() to set default values for settings and options. Keyword arguments passed will override any default or parsed values.

This simple Django utility allows you to utilize the 12factor inspired DATABASE_URL environment variable to configure your Django application.

Supported Databases

Support currently exists for PostgreSQL, PostGIS, MySQL, MySQL (GIS), Oracle, Oracle (GIS), and SQLite.

Supported Caches

Support currently exists for Redis, Memcached and local memory cache.


Installation is simple:

$ pip install django-connection-url


Configure your database in from DATABASE_URL:

import connection_url

DATABASES['default'] = connection_url.config('DATABASE_URL')

Provide an arbitrary Database URL:

DATABASES['default'] = connection_url.config('postgres://...')

Set default values and override with keyword arguments:

connection_url.config('mysql://username:password@hostname:5432/database?TIMEOUT=100', {
                      'ENGINE': 'django.contrib.gis.db.backends.postgis',
                      'CONN_MAX_AGE': 1000,
                      'OPTIONS': {'MAX_CONNECTIONS': 120},
                      }, ENGINE='django.db.backends.postgresql_psycopg2')
#produces the following result
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'database',
    'HOST': 'hostname',
    'PORT': 5432,
    'USER': 'username',
    'PASSWORD': 'password',
    'CONN_MAX_AGE': 1000,
    'OPTIONS': {'TIMEOUT': '100','MAX_CONNECTIONS': 120},

The order of precedence for dict arguments, parsed values and keyword arg overrides is: Keyword arguments > Parsed values from connection URL > Defaults from passed dict objects.

URL schema

Engine Django Backend URL
PostgreSQL django.db.backends.postgresql_psycopg2 postgres://USER:PASSWORD@HOST:PORT/NAME [1]
PostGIS django.contrib.gis.db.backends.postgis postgis://USER:PASSWORD@HOST:PORT/NAME
MySQL django.db.backends.mysql mysql://USER:PASSWORD@HOST:PORT/NAME
MySQL (GIS) django.contrib.gis.db.backends.mysql mysqlgis://USER:PASSWORD@HOST:PORT/NAME
SQLite django.db.backends.sqlite3 sqlite:///PATH [2]
Redis redis_cache.RedisCache redis://USER:PASSWORD@HOST:PORT [4]
Memcached django.core.cache.backends.memcached.MemcachedCache memcached://USER:PASSWORD@HOST:PORT
Local memory django.core.cache.backends.locmem.LocMemCache locmem://HOST:PORT/PATH [5]
[1]With PostgreSQL, you can also use unix domain socket paths with percent encoding: postgres://%2Fvar%2Flib%2Fpostgresql/dbname.
[2]SQLite connects to file based databases. The same URL format is used, omitting the hostname, and using the “file” portion as the filename of the database. This has the effect of four slashes being present for an absolute file path: sqlite:////full/path/to/your/database/file.sqlite.
[3]Note that when connecting to Oracle the URL isn’t in the form you may know from using other Oracle tools (like SQLPlus) i.e. user and password are separated by : not by /. Also you can omit HOST and PORT and provide a full DSN string or TNS name in NAME part.
[4]Requires django-redis-cache to be installed.
[5]For local memory cache, HOST:PORT/PATH is used as unique identifier for the cache. If none is given, a random UUID is used as identifier.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-connection-url, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size django-connection-url-0.1.2.tar.gz (5.4 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page