Skip to main content

Adds three template tags to Django: `fullurl`, `fullstatic` and `buildfullurl`. The template tag `fullurl` acts just like `url`, but it always prints absolute URLs with scheme and domain

Project description

django-fullurl on PyPI django-fullurl on PyPI django-fullurl on PyPI django-fullurl on PyPI Travis CI for django-fullurl (master branch) Say Thanks!

Introduction

django-fullurl adds three new template tags to Django: fullurl, fullstatic, and buildfullurl.

fullurl and fullstatic behave like url and static respectively, but they always return an absolute URL with the scheme and authority/domain parts.

For example, take this url tag:

{% url "articles:article" slug="hello" %}

In our example, this prints:

/articles/hello

This is called by some an absolute URL, because it begins with a forward-slash. However, it is not an absolute absolute URL, because it does not contain the scheme and authority parts.

If we replace url with fullurl, it will print this result:

http://example.com/articles/hello

Behind the scenes, it uses request.build_absolute_uri to determine the correct scheme and authority/domain parts.

In the same way that fullurl extends url, fullstatic extends the static template tag.

buildfullurl takes a relative URL as an argument, and prints an absolute URL with the scheme and authority parts. For example:

{% buildfullurl article.cover.url %}

Installation

Run on the command-line:

$ pip install django-fullurl

Make sure these two apps are included in your INSTALLED_APPS settings:

INSTALLED_APPS = [
    'django.contrib.staticfiles',
    'fullurl',
    # ...
]

Make sure django.template.context_processors.request is included in your context processors.

Template tags summary

  • {% fullurl "some-url-name" v1 v2 %} This behaves like the url Django template tag (doc), but it returns a full URL instead of a relative one.

  • {% fullstatic "images/hi.jpg" %} This behaves like the static Django template tag (doc), but it returns a full URL instead of a relative one.

  • {% buildfullurl "/foobar" %} This will convert a relative URL into a full URL using request.build_absolute_uri (doc).

Example usage

OpenGraph

OpenGraph URLs need to be absolute, including scheme and authority parts. Here’s how you can use fullurl and fullstatic to help with this:

{% load fullurl %}

<meta property=<s>"og:url"</s> content=<s>"</s>{% fullurl "articles:article" article=article %}<s>"</s>>
<meta property=<s>"og:image"</s> content=<s>"</s>{% fullstatic "cat.jpg" %}<s>"</s>>

You can convert a URL from relative to absolute using buildfullurl tag:

{% load fullurl %}

<meta property=<s>"og:url"</s> content=<s>"</s>{% buildfullurl article.get_absolute_url %}<s>"</s>>
<meta property=<s>"og:image"</s> content=<s>"</s>{% buildfullurl article.image.url %}<s>"</s>>

See also

Here are some alternatives, in alphabetical order:

  • django-absoluteuri (GitHub, Django Packages) provides two template tags: absoluteuri and absolutize. Unlike this app, it uses the site framework to determine the domain to use, which could be an additional database query. This can be useful in some situations, for instance when creating emails.

  • django-absoluteurl (GitHub, Django Packages) provides a template tag absoluteurl that works in a similar fashion as fullurl. It does not seem to work in Python 2, or with newer Django versions, however.

  • django-full-url (GitHub) allows you to get specific parts of the current URL using code like {{ url_parts.domain }}, among other things. It does not provide the same functionality as this app, the naming similarity was unintentional.

  • django-urltags (Django Packages) provides a few template tags, including absurl, but it hasn’t been updated since 2012 and it doesn’t seem to work on recent Django versions.

Download files

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

Source Distribution

django-fullurl-1.3.tar.gz (6.6 kB view hashes)

Uploaded source

Built Distribution

django_fullurl-1.3-py2.py3-none-any.whl (6.5 kB view hashes)

Uploaded py2 py3

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