Templates and tags in Django Bindings

Django Bindings has been deprecated. For more, see the Deprecation Notice.

Django Bindings implements Django's template system to server-side render data into HTML pages.

When you create a Web Framework app using Django Bindings, you create a page template that corresponds to each page in the app. For example, when you create a Splunk Web Framework app, a home.html template is automatically created to render the Home page of your app. To add a new page to your app, you create a new page template.

The Web Framework uses the Django template language to define the elements, or tags, of a template. Tags represent both the data to display and the structure of the document. To render the page, these tags call a Python function that creates a <div> section on the rendered HTML page with the corresponding information. Tags can also be used for text manipulation, to perform flow-control loops, to load external information, and more.


Common tags

This section describes the common tags used most in the Web Framework. See the Django documentation for in-depth information about tags in general.

Typically in the Web Framework, tags that display data (such as a search manager or Splunk view) or perform an action (such as loading a template) take this format:

{% tagname %}

Tags that represent the document structure (for example, sections for CSS or JavaScript) use a block format to surround their contents like this:

{% block tagname %}
{% endblock tagname %}

Comments can be used in this format:

{# This is a comment #}

To display a variable on a template, use double curly brackets:

{{ variable }}
{{ variable.attribute }}

Here are some special variables used by the Web Framework:

  • {{app_name}} is used for your app's name.
  • {{STATIC_URL}} is the URL path to the deployed version of the static path /dj/static/ and static handler.
  • {{SPLUNKWEB_URL_PREFIX}} is used for the prefix of internal URLs. When the page is rendered, this placeholder is replaced with the correct URL for users, including their locale.

Use this template tag format to inherit from a base template:

{% extends "template_name.html" %}

Use this template tag format to load a template and render it with the current app context:

{% include "template_name.html" %}

Use this template tag to load the Web Framework tags and filter libraries:

{% load splunkmvc %}

Use this template tag to link to another page in the same app (this format only works when you've added a URL mapping and view for the page):

<a href="{% url 'your_app_name:page_name' %}>your link text</a>


Boilerplate template

Now to put it all together. Here's a boilerplate page template that shows the tags and structure that a typical Web Framework template uses. You can copy and paste this code to use as a starting point whenever you create new page templates.

{# Boilerplate for a Django Bindings template #}

{% extends "splunkdj:base_with_app_bar.html" %}

{% load splunkmvc %}

{% block title %}{{app_name}} Your page title goes here{% endblock title %}

{% block css %}
    <!-- Style sheets are loaded here -->
    <link rel="stylesheet" type="text/css" href="{{STATIC_URL}}{{app_name}}/custom.css" />
    <link rel="stylesheet" type="text/css" href="{{STATIC_URL}}splunkjs/css/dashboard.css" />
        /* Define any page styles here*/
{% endblock css %}

{% block content %}
    <!-- You can use HTML and <div> tags for layout -->
    {# Splunk views go here #}
{% endblock content%}

{% block managers %}
    {# Search managers go here #}
{% endblock managers %}

{% block js %}
    {# JavaScript goes here #}
{% endblock js %}