Not Logged In

django-url-tools 0.0.8

------------------------------------------------------Django helper tools for dealing with URLs in templates------------------------------------------------------

.. contents::

Overview========

Django URL tools are context processors, and template tags that help you dealwith URL manipulations in templates. The heavy lifting is done by the``url_tools.helper.UrlHelper`` class which wraps around ``urllib``,``urlparse``, and Django's ``QueryDict`` to provide facilities for parsing andmanipulating URLs.

If you want to use the template tags, add ``url_tools`` to installed apps::

INSTALLED_APPS = ( ... 'url_tools', )

UrlHelper class===============

``UrlHelper`` class implements all methods for manipulating URLs that are usedin other parts of this app. You can also use this class directly by importingit from the ``helper`` module::

from url_tools.helper import UrlHelper

``UrlHelper`` constructor accepts only one argument, which is the full path ofthe URL you want to manipulate. Although we can technically make ``UrlHelper``deal with full absolute URLs, we have opted to implement only methods fordealing with paths instead. Therefore, if you pass UrlHelper an full URL withscheme, host, port, and user credentials, it would still only use the path,query parameters, and the fragment identifiers.

You can also pass an instance of ``UrlHelper`` class to the constructor if youneed to do so.

The class has following properties:

+ ``path``: URL's path without query string and fragment identifier+ ``fragment``: URL's fragment identifier (without the pound character ``#``)+ ``query_dict``: ``QueryDict`` instance containing the URL's query parameters+ ``query``: similar to ``query_dict`` but also does more when assigning+ ``query_string``: URL's query string+ ``hash``: MD5 hexdigest of the full path including query parameters

UrlHelper.path--------------

This is a simple string property containing the URl's path. For example, in anURL ``'/foo/bar?baz=1#boo'``, the property contains ``'/foo/bar'``.

Returns the MD5 hexdigest of the full path including query parameters. This canbe useful for use with caching and other situations where we need todifferentiate same paths with different query parameters. ::

This method returns the query string using ``QueryDict``'s ``urlencode()``method. Any keyword parameters you pass to this method are forwarded to the``urlencode()`` method. Currently, the only keyword argument is ``safe`` whichinstructs the method to not escape specified characters.

UrlHelper.get_query_data()--------------------------

Returns the ``UrlHelper.query_dict`` property. This methods exist mostly tohelp customize the behavior of ``UrlHelper.query`` in subclasses, since thegetter calls this method instead of returning the ``query_dict`` propertydirectly.

This method takes any number of keyword arguments and updates the``UrlHelper.query_dict`` instance. Since, unlike Python dictionary, each``QueryDict`` key can have multple values, you can pass multiple values asPython iterables such as lists or tuples. For example::

This method adds or removes query parameters depending on whether theyalready exist. It looks for both a matching parameter and value, andadds new parameters using ``UrlHelper.overload_params``. Here isa simple example::

Delete multiple parameters. If no parameters are specified, _all_ parametersare removed. You can also specify a set of key-value pairs to remove specificparameters with specified _values_. Here are a few examples::

The ``current_url`` context processor will add a new variable to the template'scontext. This variable is called ``current_url``, and it's an ``UrlHelper``instance. Therefore, this variable has all the properties and methods of the``UrlHelper`` class. For instance, if we are currently on ``/foo/bar?baz=1``path, you can do the following in a template::

and so on. The variable itself renders as full relative path with query stringand fragment identifier (identical to output of ``UrlHelper.get_full_path()``method).

Template tags=============

To use the template tags, first load the ``urls`` library::

{% load urls %}

URL tools currently has only one template tag, which is an assignment tag.

{% add_params %}----------------

This template tag outputs a path with query string parameters given as keywordarguments. For instance, if we are on a page at ``/foo``, we can use this tag::

{% add_params request.get_full_path foo='bar' %}

and the output would be::

/foo?foo=bar

Existing URL parameters are overridden by the ones specified as keywordarguments.

{% overload_params %}---------------------

Similar to ``{% add_params %}`` tag, except that it does not update existingparameters but overloads them with new values. For example, if we are on apage at ``/foo?bar=1``, we can use this tag like so::

{% overload_params request.get_full_path bar=2 %}

and the output would be::

/foo?bar=1&bar=2

{% del_params %}----------------

This tag outputs a path stripped of specified parameters, or all query parameters if none are specified. If you use keyword arguments, only thespecified name-value pairs will be removed.

For example, if we are on the ``/foo?bar=1&bar=2&baz=2`` URL::

{% del_param request.get_full_path 'bar' %}

outputs::

/foo?baz=2

and ::

{% del_params request.get_full_path %}

outputs::

/foo

Finally::

{% del_params request.get_full_path bar=2 %}

outputs::

/foo?bar=1&baz=2

{% toggle_params %}---------------------

This tag adds or removes parameters, depending on whether the parameter andvalue exists. For example, if we are on a page at ``/foo?bar=1``, we can toggle the state of ``bar=1`` using::

{% toggle_params request.get_full_path bar=1 %}

and the output would be::

/foo

If we are on a page at ``/foo``, then the output of would be::

/foo?bar=1

Multiple parameters and values can be used. For example, on a page at``/foo?bar=1&foo=2``, parameters can be toggled like this::

{% toggle_params request.get_full_path bar=3 foo=2 foo=3 %}

To give::

/foo?bar=1&bar=3&foo=3

{% url_params %}----------------

This tag is used as an assignment tag. Its first argument is an URL, followedby any number of keyword arguments that represent the URL parameters. Forexample, if we are requesting a page on ``'/foo'`` path, and do this::

{% url_params request.get_full_path foo='bar' as new_url %}

We can use the ``new_url`` variable from that point on, that represents the``/foo?foo=bar`` URL. To use this with your configured URLs, you can use thebuilt-in ``url`` tag::

If the reverse match for ``'foo'`` is, say, ``'/foo'``, then the ``foo_url``variable will, predictably, contain ``'/foo?foo=bar'``.

This tag will override existing parameters rather than adding new values forexisting keywords. Therefore, you can safely use it to set URL parameterswhether they exist or not. This is typically useful when you are building URLsfor controls like pagers. Regardless of whether there is a ``page`` parameteror not, setting it with ``url_params`` tag will correctly set the parameter todesired value::

{% url_params current_url page=2 %} {# this works for both ``/foo?page=1`` and just ``/foo`` #}

Template filters================

URL tools also include filters for manipulating data that will be used as partof URLs. To use them, you need to load the ``urls`` library first::

{% load urls %}

quote-----

The ``quote`` filter quotes URL parameters. It accepts optional safe charactersthat can be used to prevent quoting of certain characters. This filter uses`urllib.quote`_ for quoting. Safe characters inlude only the slash ``/`` bydefault. ::

{{ value|quote:"~/" }}

quote_plus----------

The ``quote_plus`` filter is similart ot the `quote`_ filter, except that itconverts all spaces to ``+``. This filter also takes optional safe characters. The filter uses `urllib.quote_plus`_ for quoting. ::

{{ value|quote_plus }}

Reporting bugs==============

Please report any bugs to our BitBucket `issue tracker`_.

Contributors============

We thank the following contributors:

+ nlaurance_ for contributing the ``overload_params`` and improvements to ``del_params``, as well as compatibility with Django 1.4.x.