Coder Social home page Coder Social logo

django-absoluteuri's Introduction

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/'

django-absoluteuri's People

Contributors

acatton avatar selectnull avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.