How to create a URL mapping and view for a template

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

The easiest way to show how to create a URL mapping and view for a template is with an example. Here's how to add a single "helloworld" template to an existing app (your_app_name), along with a URL mapping and view.

 Create a template for your app

  1. In the $SPLUNK_HOME/etc/apps/your_app_name/django/your_app_name/templates directory, create a new file called helloworld.html.
  2. Open helloworld.html in a text editor and paste in this text:
  3. Hello, world!
    
  4. Save the file.

 Add a URL mapping for the template

  1. In a text editor, open the urls.py file (located in the $SPLUNK_HOME/etc/apps/your_app_name/django/your_app_name directory). This file already contains the definition for the "home" URL.
  2. The format of URL patterns for app templates is:

    urlpatterns = patterns('',
        url(URL 1, function_name 1, page_name 1),
        . . .
        url(URL n, function_name n, page_name n),
    )
    

    The URL pattern contains these components:

    • URL: A regex indicating the URL pattern to match.
    • function_name: A reference to the corresponding Django view for the template, in the format your_app_name.views.function_name.
    • page_name: The name used for internal and programmatic references to the page template.
  3. Add a URL mapping for the new template as follows (insert the text shown in bold). For simplicity in this example, we'll use "helloworld" as the page name in the URL, as the view name, and as the name for references (you supply your_app_name). Be careful to match the indentation, using only spaces or only tabs consistently—it matters in Python:
  4.     from django.conf.urls import patterns, include, url
        from splunkdj.utility.views import render_template as render
        urlpatterns = patterns('',
            url(r'^home/$', 'your_app_name.views.home', name='home'),
            url(r'^helloworld/$', 'your_app_name.views.helloworld', name='helloworld'),
        )
    
  5. Save the file.

 Add a Django view for the template

  1. In a text editor, open the views.py file (located in the $SPLUNK_HOME/etc/apps/your_app_name/django/your_app_name directory). This file already contains the definition for the "home" view.
  2. The format for view functions is:

    @render_to(template_name_to_render_the_page_in)
    @login_required
    def function_name(request):
        return {
            parameters
        }
    
      Note  Due to Python syntax, function_name cannot contain dashes (-).
  3. Append the following code for the new view to the end of the views.py file as follows (the return block is empty because we aren't sending any parameters to the template):
  4.     @render_to('your_app_name:helloworld.html')
        @login_required
        def helloworld(request):
            return {}
    
  5. Save the file.

 View the new template

  1. Restart Splunk Web (you need to restart Splunk Web any time you edit a *.py file), which you can do by opening the http://<localhost:port>/debug/refresh URL in your web browser and clicking Refresh.
  2. Open the new page of your app (http://<localhost:port>/dj/your_app_name/helloworld/) in a web browser. After you log in, you should see "Hello, world!".