URL mapping and Django views

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

Django Bindings uses Django's URL mappings and Django views to direct URLs to the correct page templates in your Web Framework apps. When you create a new Web Framework app, a URL mapping and view are automatically created for the default Home page. And by default, the Web Framework automatically tries to redirect any URL in the form http://<localhost:port>/dj/your_app_name/template_name to the corresponding template in the app. So, you aren't required to modify or create a URL mapping or view for most templates you create.

However, if you want to add your own logic or use custom variables in templates whose values are generated by Python code, creating a custom view and URL mapping is necessary. For example, you could write code that redirects to a template based on URL parameters or that passes a value to the template, so you would need to create your own URL mappings and views.

 

URL mapping

The URL to each page in your Web Framework app is defined by adding a URL pattern to the urls.py file, which is a Python module that is located in your app's directory ($SPLUNK_HOME/etc/apps/your_app_name).

The format of URL patterns 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.

So for an app called "testapp", the Web Framework creates this URL for your app's Home page:

urlpatterns = patterns('',
    url(r'^home/$', 'testapp.views.home', name='home'),
)

According to this URL pattern:

  • r'^home/$' corresponds to your root URL followed by "home". On your local computer, the testapp home page URL would be http://<localhost:port>/dj/testapp/home.
  • 'testapp.views.home' maps the Home page URL to a Django view named "home".
  • name='home' indicates that the internal name of the page template for this URL is "home". For example, you could use a URL template tag {% url 'testapp:home' %} from another page in your app to link back to the home page.

To add another page to your app called "startup" (for the URL http://<localhost:port>/dj/testapp/startup), with a view called "startup_view" and page name of "startup_page", you'd add the following line to the URL pattern:

urlpatterns = patterns('',
    url(r'^home/$', 'testapp.views.home', name='home'),
    url(r'^startup/$', 'testapp.views.startup_view', name='startup_page'),
)
    Note  Any time you edit the urls.py file, you'll need to restart Splunk Web for your changes to take effect, which you can do by opening the http://<localhost:port>/debug/refresh URL in your web browser and clicking Refresh.

For more about URL patterns, see URL Dispatcher on Django's website.

 

Django views

A Django view is the Python function that is called according to a particular URL mapping for an app page (for example, the home page URL http://<localhost:port>/dj/mycoolapp/home is mapped to a Django view). Each Django view in your Web Framework app needs to be defined in the views.py file, which is a Python module that is located in your app's directory ($SPLUNK_HOME/etc/apps/your_app_name).

The format of a typical Django view in the Web Framework is:

@render_to('your_app_name:template_name.html')
@login_required
def view_name(request):
    # ...python code...
    return {
        parameters
    }
    Note  Due to Python syntax, the function name view_name cannot contain dashes (-).

This view includes two decorators:

  • The @render_to decorator determines which template to render the page with. This template can be within the app, or specify a different namespace (your_app_name) to use a template from a different app.
  • The @login_required decorator requires the user to log in to Splunk before going to the app. Under the covers, the Web Framework shares the session login information for Splunk instances. So, if the user has not yet logged in to Splunk (through Splunk Web or any other app), they see the Splunk login page first. If they are logged in already, they go directly to the app.

So for an app called "testapp", this Django view is created for the app's Home page:

@render_to('testapp:home.html')
@login_required
def home(request):
    return {
        "message": "Hello World from testapp!",
        "app_name": "testapp"
    }

The returned values are sent to the specified template, allowing a template to insert the variable using the {{ message }} or {{ app_name }} tags. This example sends a message and an app_name variable to the home.html template.

You can also include code because the views.py is a Python module. Here's an example using a view called "startup_view". This Django view creates a today variable that contains the current date as a string, and sends it to the specified template, startup_template.html:

import datetime
...
@render_to('testapp:startup_template.html')
@login_required
def startup_view(request):
    now = datetime.datetime.now()
    today = now.strftime("%A, %b %d, %Y")
    return {
        "today": today
    }

To interact with Splunk programmatically, use the Splunk SDK for Python. Here's an example that retrieves the collection of search jobs from your Splunk instance and returns the jobs collection object to the startup template:

@render_to('testapp:startup_template.html')
@login_required
def startup_view(request):
    service = request.service
    jobs = service.jobs
    return {
        "jobs": jobs
    }
    Note  Any time you edit the views.py file, you'll need to restart Splunk Web for your changes to take effect, which you can do by opening the http://<localhost:port>/debug/refresh URL in your web browser and clicking Refresh.