Giter Club home page Giter Club logo

django-absolute's Introduction

Django Absolute

image

Django Absolute provides context processors and template tags to use full absolute URLs in templates.

Installation

You can install Django Absolute with pip:

pip install django-absolute

or with easy_install:

easy_install django-absolute

Add absolute to your settings.INSTALLED_APPS.

Context processor

Add absolute.context_processors.absolute to your settings.TEMPLATE_CONTEXT_PROCESSORS. Django Absolute context processor depends on request context processor:

from django.conf import global_settings

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    'django.core.context_processors.request',
    'absolute.context_processors.absolute',
)

Then you can access the following variables in your templates:

  • ABSOLUTE_ROOT: full absolute root URL (without trailing slash) based on incoming request
  • ABSOLUTE_ROOT_URL: full absolute root URL (with trailing slash) based on incoming request
  • SITE_ROOT: full absolute root URL (without trailing slash) based on current Django Site
  • SITE_ROOT_URL: full absolute root URL (with trailing slash) based on current Django site

Template tags

Django absolute provide 2 template tags:

  • absolute: acts like url but provide a full URL based on incoming request.
  • site: acts like url but provide a full URL based on current Django Site.

To use theses template tags, you need to load the absolute template tag library.

{% load absolute %}

{% url index %}
{% absolute index %}
{% site index %}

These template tags have exactly the same syntax as url, including the "as" syntax:

{% absolute index as the_url %}
{{ the_url }}

If you use Django 1.5, you need to use the "new-style" url syntax (quoted parameters):

{% load absolute %}

{% url "index" %}
{% absolute "index" %}
{% site "index" %}

{% absolute "index" as the_url %}
{{ the_url }}

If you want to match the "new-style" syntax in Django < 1.5 you need to load absolute_future instead (same behavior as {% load url from future %}).

{% load url from future %}
{% load absolute_future %}

{% url "index" %}
{% absolute "index" %}
{% site "index" %}

{% absolute "index" as the_url %}
{{ the_url }}

For more informations, see the Django 1.5 release notes.

django-absolute's People

Contributors

elijen avatar noirbizarre avatar rodrigoprimo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

django-absolute's Issues

Requires 'request' in ctx

You app is fine, but what about using site templatetag for example in e-mail templates. I don't have request in context there.

The request is only needed in order to return protocol, I think it should fallback to http when unable to detect.

KeyError request['context'] in AbsoluteURLNode

I have an issue with the render method of AbsoluteURLNode.
It doesn't find the context['request'] when being rendered...
Any help would be welcomed!

File designs/templatetags/email.py line 14 in render:
request = context['request']
File django/template/context.py line 56 in getitem:
raise KeyError(key)
KeyError: 'request'

This comes from the code:

class AbsoluteUrlNode(URLNode):
    def render(self, context):
        asvar, self.asvar = self.asvar, None  # Needed to get a return value from super
        path = super(AbsoluteUrlNode, self).render(context)
        self.asvar = asvar
        request = context['request']
        absolute_url = request.build_absolute_uri(path)
        if asvar:
            context[asvar] = absolute_url
            return ''
        else:
            return absolute_url```

Looking for maintainers

Hi everyone !

It's been a pleasure to develop and maintain this application for Django.
Unfortunately, I can't maintain it anymore, mostly because I'm not using Django anymore and I don't have the time to follow the new versions.

I will very soon migrate this repository here: https://github.com/djangojs

I'm currently looking for maintainers so anyone interested in, please comment here.

pip install pip install django-absolute==0.2 fails

Thanks for fixing #1, nice one, but...

(_env)[piotr@piotr-amd tmp]$ pip install django-absolute==0.2
Downloading/unpacking django-absolute==0.2
  Running setup.py egg_info for package django-absolute
    Traceback (most recent call last):
      File "<string>", line 16, in <module>
      File "/home/piotr/tmp/_env/build/django-absolute/setup.py", line 19, in <module>
    rst('CHANGELOG.rst'),
      File "/home/piotr/tmp/_env/build/django-absolute/setup.py", line 13, in rst
    content = open(filename).read()
    IOError: [Errno 2] No such file or directory: 'CHANGELOG.rst'
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 16, in <module>

  File "/home/piotr/tmp/_env/build/django-absolute/setup.py", line 19, in <module>

    rst('CHANGELOG.rst'),

  File "/home/piotr/tmp/_env/build/django-absolute/setup.py", line 13, in rst

    content = open(filename).read()

IOError: [Errno 2] No such file or directory: 'CHANGELOG.rst'

----------------------------------------
Command python setup.py egg_info failed with error code 1 in /home/piotr/tmp/_env/build/django-absolute
Storing complete log in /home/piotr/.pip/pip.log

pip install -e ... for 0.3dev is fine

Rendering tag in `as` syntax (0.2.1)

These template tags have exactly the same syntax as url.

That is not exactly true. When you are using {% url .... as variable %} the 'content' is not rendered, but in absolute is.

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.