Common utitlities

Current site, STATIC_URL context processors

Created on 10.8.2011

@author: xaralis

fragapy.common.context_processors.current_site(request)

A context processor to add the current_site to the current Context.

fragapy.common.context_processors.static(request)

Adds STATIC_URL to template context. Beneficial for pre-1.3 Django.

Middleware for limiting access, shortening HTML responses

class fragapy.common.middleware.PermissionMiddleware

Special middleware for sites that should not be seen by general public.

If REMOTE_ADDR is not in INTERNAL_IPS setting, login form will be displayed and the person would not be let in until it fills the form correctly.

class fragapy.common.middleware.SpacelessMiddleware

Add to MIDDLEWARE_CLASSES when you want to have all of your HTML reponses to be smaller.

Reduces the size of HTML response by applying strip_spaces_between_tags always.

{% macro %} templatetag

Tag library that provides support for “macros” in Django templates.

Usage example:

  1. Save this file as

    <yourapp>/taglibrary/macros.py

  2. In your template load the library:

    {% load macros %}

  3. Define a new macro called ‘my_macro’ with parameter ‘arg1’:

    {% macro my_macro arg1 %} Parameter: {{ arg1 }} <br/> {% endmacro %}

  4. Use the macro with a String parameter:

    {% usemacro my_macro “String parameter” %}

    or with a variable parameter (provided the context defines ‘somearg’ variable, e.g. with value “Variable parameter”):

    {% usemacro my_macro somearg %}

    The output of the above code would be:

    Parameter: String parameter <br/> Parameter: Variable parameter <br/>

  5. Alternatively save your macros in a separate file, e.g. “mymacros.html” and load it to the current template with:

    {% loadmacros “mymacros.html” %}

    Then use these loaded macros in {% usemacro %} as described above.

Macros can take zero or more arguments and both context variables and macro arguments are resolved in macro body when used in {% usemacro ... %} tag.

Bear in mind that defined and loaded Macros are local to each template file and are not inherited through {% extends ... %} tags.