Skip to main content

Absolute URI functions and template tags for Django

Project description

django-absoluteuri

https://travis-ci.org/fusionbox/django-absoluteuri.png?branch=master

Absolute URI functions and template tags for Django.

Why

There are times when you need to output an absolute URL (for example, inside an email), but you don’t always have access to the request. These utilities use the Sites Framework if available in order to create absolute URIs.

Installation

Install django-absoluteuri:

pip install django-absoluteuri

Then add it to your INSTALLED_APPS:

INSTALLED_APPS = (
    # ...
    'django.contrib.sites',
    'absoluteuri',
)

django-absoluteuri requires the Sites Framework to be in INSTALLED_APPS well and configured as well.

Settings

The protocol of the uris returned by this library defaults to http. You can specify the protocol with the ABSOLUTEURI_PROTOCOL setting.

# settings.py
ABSOLUTEURI_PROTOCOL = 'https'

# Elsewhere
>>> absoluteuri.build_absolute_uri('/some/path/')
'https://example.com/some/path/'

Template Tags

There are two template tags, absoluteuri and absolutize. absoluteuri works just like the url tag, but that it outputs absolute URLs.

{% load absoluteuri %}

<a href="{% absoluteuri 'my_view' kwarg1='foo' kwarg2='bar' %}">click here</a>

absolutize will take a relative URL and return an absolute URL.

{% load absoluteuri %}

<a href="{% absolutize url_from_context %}">click here</a>

Filter

Sometimes instead of template tags, it’s easier to use filters. You can do that as well.

{% load absoluteuri %}

<a href="{{ my_object.get_absolute_url|absolutize }}">click here</a>

But there are situations where tag can not be used but filter can.

{% load absoluteuri %}

{% include "some-other-template.html" with url=my_object.get_absolute_url|absolutize %}

Functions

There are also two functions that django-absoluteuri provides, build_absolute_uri and reverse, which are equivalents of request.build_absolute_url and urlresolvers.reverse.

>>> import absoluteuri

>>> my_relative_url = '/path/to/somewhere/'
>>> absoluteuri.build_absolute_uri(my_relative_url)
'http://example.com/path/to/somewhere/'
>>> absoluteuri.reverse('viewname', kwargs={'foo': 'bar'})
'http://example.com/path/to/bar/'

Changelog

1.3.0 (2018-09-04)

  • Add support for Django 2.1. Remove support for Django < 1.11.

1.2.0 (2016-02-29)

  • Add absolutize filter. This deprecates the absolutize tag. [#4]

1.1.0 (2015-03-23)

  • Added ABSOLUTEURI_PROTOCOL settings. [#1]

  • Documented sites framework requirement.

1.0.0 (2015-03-17)

  • First release on PyPI.

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-absoluteuri-1.3.0.tar.gz (7.9 kB view details)

Uploaded Source

File details

Details for the file django-absoluteuri-1.3.0.tar.gz.

File metadata

  • Download URL: django-absoluteuri-1.3.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6rc1

File hashes

Hashes for django-absoluteuri-1.3.0.tar.gz
Algorithm Hash digest
SHA256 2b7cb57748b4f557d2cdb1f74d6cfa197bed7f664b9c2a039094575a1b648e4a
MD5 b9494044d5c4cbd7d702e3c901111c58
BLAKE2b-256 da92eb37eaef8f099c6ceccc4e9d86629dc99a5a228ef1c9215ee66a56ee0d1c

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