websauna.system.core.templatecontext module

Websauna template filters .

websauna.system.core.templatecontext.admin_url(jinja_ctx, model_instance, *elements, **kw)[source]

Link to model in admin interface.

Takes an SQLAlchemy model instance as a filter argument and resolves its admin page. This requires that a model admin has been correctly registered for SQLAlchemy model.


{% if request.user and request.user.is_admin %}
      <a class="btn btn-danger" href="{{ choice|admin_url("edit") }}">
        Edit in admin
{% endif %}

Another example:

  <a class="btn btn-danger" href="{{ choice|admin_url }}">
    View in admin
websauna.system.core.templatecontext.arrow_format(jinja_ctx, context, *args, **kw)[source]

Format datetime using Arrow formatter string.

Context must be a time/datetime object.

Arrow is a Python helper library for parsing and formatting datetimes.


  Offer created at {{ offer.created_at|arrow_format('YYYYMMDDHHMMss') }}

See Arrow formatting.

websauna.system.core.templatecontext.escape_js(jinja_ctx, context, **kw)[source]

Make JSON strings to safe to be embedded inside <script> tag.

websauna.system.core.templatecontext.filter_datetime(jinja_ctx, context, **kw)[source]

Format datetime in a certain timezone.

websauna.system.core.templatecontext.friendly_time(jinja_ctx, context, **kw)[source]

Format timestamp in human readable format.

  • Context must be a datetimeobject

  • Takes optional keyword argument timezone which is a timezone name as a string. Assume the source datetime is in this timezone.

websauna.system.core.templatecontext.from_timestamp(jinja_ctx, context, **kw)[source]

Convert UNIX datetime to timestamp.


    Prestodoctor license expires: {{ prestodoctor.recommendation.expires|from_timestamp(timezone="US/Pacific")|friendly_time }}

context – UNIX timestamps as float as seconds since 1970


Python datetime object

websauna.system.core.templatecontext.include_filter(config, name, func, renderers=('.html', '.txt'))[source]

Register a new Jinja 2 template filter function.


import jinja2

def negative(jinja_ctx:jinja2.runtime.Context, context:object, **kw):
    '''Output the negative number.


        {{ 3|neg }}

    neg = -context
    return neg

Then in your initialization::

include_filter(config, "neg", negative)
  • config (Configurator) – Pyramid configurator

  • name (str) – Filter name in templates

  • func (Callable) – Python function which is the filter

  • renderers – List of renderers where the filter is made available

websauna.system.core.templatecontext.render_panel(jinja_ctx, context, name, **kwargs)[source]

Render a panel inline in a template.

Allows placing admin panels in templates directly.

Example how to include panel at the top of admin CRUD listing template:

{% block title %}


  {{ context|render_panel(name="admin_panel", controls=False) }}

{% endblock %}
  • context – Any resource object, like ModelAdmin instance

  • name – registered panel name, like admin_panel

  • kwargs – Passed to the panel function as is


HTML string of the rendered panel

websauna.system.core.templatecontext.timestruct(jinja_ctx, context, **kw)[source]

Render both humanized time and accurate time.

  • show_timezone

  • target_timezone

  • source_timezone

  • format

websauna.system.core.templatecontext.to_json(jinja_ctx, context, safe=True)[source]

Converts Python dict to JSON, safe to be placed inside <script> tag.


  Export server side generated graph data points
  to Rickshaw client side graph rendering
{% if graph_data %}
    window.graphDataJSON = "{{ graph_data|to_json }}";
{% endif %}
  • context – Takes Python dictionary as input

  • safe – Set to False to not to run escape_js() on the resulting JSON. True by default.


JSON string to be included inside HTML code

websauna.system.core.templatecontext.uuid_to_slug(jinja_ctx, context, **kw)[source]

Convert UUID object to a base64 encoded slug.


{% for question in latest_question_list %}
      <a href="{{ route_url('details', question.uuid|uuid_to_slug) }}">
        {{ question.question_text }}
{% endfor %}