Dashboards & Visualizations

How do you trigger a search with a button using Django in the web framework?

lquinn
Contributor

I am trying to create a view using django which has text input boxes and a submit button. The user fills in the text input boxes and then clicks the submit button. When the button is clicked I would like a search to be triggered using the information that was inputted in the text input boxes. My code is the following:

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

{% load splunkmvc %}

{% block title %}{{app_name}} Home Page{% endblock title %}

{% block css %}
<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" />
<style>        
</style>
{% endblock css %}

{% block content %}
<div class="dashboard-body container-fluid main-section-body">
    <div>
        <h3>High value:</h3>{% textinput id="high-input" value="$high$"|token_safe %}
        <h3>Medium value:</h3>{% textinput id="medium-input" value="$medium$"|token_safe %}
        <h3>Low value:</h3>{% textinput id="low-input" value="$low$"|token_safe %}  
    </div>
    <div >
        <button id="search_btn" onclick="myfunction()">Search</button>
    </div>
</div>

{% endblock content%}

{% block managers %}
{% searchmanager
    id="outputsearch"
    search="some search ..."|token_safe
    autostart=False
%}
{% endblock managers %}

{% block js %}
<script>

function myfunction(){
    require([
            "jquery",
            "splunkjs/splunk",
            "splunkjs/ready!",
            "splunkjs/mvc/searchmanager"
        ], function(mvc) {
                alert('function called ..');
                var manager = mvc.Components.get("outputsearch");
                manager.startSearch();
                alert('search started ...');
        });
    }
</script>
{% endblock js %}

When the submit button is clicked the first alert appears, however the second one does not follow, therefore there must be an issue with the following line:

var manager = mvc.Components.get("outputsearch");

I have also tried another way of listening for a click of the submit button. When using this way, the above line works successfully however it does not pick up when the button is clicked. Any help would be much appreciated!

0 Karma
1 Solution

lquinn
Contributor

Silly mistake! Its seems I missed out "splunkjs/mvc" in the require part of my function!

View solution in original post

0 Karma

lquinn
Contributor

Silly mistake! Its seems I missed out "splunkjs/mvc" in the require part of my function!

0 Karma
Get Updates on the Splunk Community!

Earn a $35 Gift Card for Answering our Splunk Admins & App Developer Survey

Survey for Splunk Admins and App Developers is open now! | Earn a $35 gift card!      Hello there,  Splunk ...

Continuing Innovation & New Integrations Unlock Full Stack Observability For Your ...

You’ve probably heard the latest about AppDynamics joining the Splunk Observability portfolio, deepening our ...

Monitoring Amazon Elastic Kubernetes Service (EKS)

As we’ve seen, integrating Kubernetes environments with Splunk Observability Cloud is a quick and easy way to ...