0

After trawling the internet for hours trying to no avail, I've come here in hope that maybe somebody can steer me in the correct direction.

So I'm new to django and stuck trying to use ajax in a template to pass data to a view. I have drop down menus that are populated from a database and when the user selects the type of result and model, I'm just trying to get it to pass these values to the view method. The reason why I'm doing this is because the view page populates the template (using extends), by generating an html page via a perl script that accepts these variables (result, model).

view.py

 def platform_pass_rate(request):
 #some other stuff
 #This is currently always true
    if request.method == 'GET':
        #It is an issue however, because this never gets anything, just as request.GET.items() returns nothing
        resultVar = request.GET['resultValue']
        # modelVar = request.POST['selectedModelValue']
        subprocess.call(["perl", "static/static/perl/passRateByPlatform.pl", resultVar, "Ahmed_00"])
        #This calls the perl script. Currently set up so that just a default result is entered - so I now GET is working
    else:
        subprocess.call(["perl", "static/static/perl/passRateByPlatform.pl", "liftforce", "Ahmed_25"])
        #Default html page created by script when user does not select anything
    current_url = get_full_path(request)
    return render_to_response("data_form_platform.html", {'our_url': current_url, 'study_models': study_models,'study_results': study_results})

urls.py

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('AppFalconV1.views',
    # Examples:
    url(r'^$', 'home'),
    url(r'^Pass_Rate_by_Build/$', 'build_pass_rate'),
    url(r'^Pass_Rate_by_Platform/$', 'platform_pass_rate'),
    url(r'^Platform_Coverage/$', 'platform_coverage'),
    url(r'^Regression_Coverage/$', 'regression_coverage'),
    url(r'^Coverage_All_Builds/$', 'coverage_all_builds'),
    # url(r'^test/$', 'get_current_url'),
    url(r'^time/$', 'current_datetime'),
    #url(r'^time/plus/(\d{1,2})/$', 'hours_ahead'),

    url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

Revised script in base.html

<script type="text/javascript">

    $(document).ready(function () {
        {#        $.ajaxSetup({ cache: false });#}
        var current_url = $('#current_url').text();
        var id = current_url.trim();
        $('#' + id).addClass('active');

        $('#platform_data_form_button').click(function () {
            var selectedResultValue = $('#platform_type_of_result :selected').text();
            console.log(selectedResultValue);
            var selectedModelValue = $('#platform_models :selected').text();
            console.log(selectedModelValue);
            $('#platform_type_of_result').change(function () {
                var selectedResultValue = $('#platform_type_of_result :selected').text();
                console.log(selectedResultValue);
            });
            $('#platform_models').change(function () {
                var selectedModelValue = $('#platform_models :selected').text();
                console.log(selectedModelValue);
            });

            $.get("/Pass_Rate_by_Platform/", { resultValue: selectedResultValue, modelValue: selectedModelValue}, function (response) {
                console.log("workinggggggg");
                //alert(response);
            });

        });
    });
</script>

Location of dropdown buttons - data_form_platform.html

{% extends 'platform_pass_rate.html' %}
{% block data_form_platform_content %}
    <form class="form-horizontal">
        <fieldset>

            <div class="form-group">
                <label class="col-md-2 control-label" for="type_of_result">Type of result</label>

                <div class="col-md-3">
                    <select id="platform_type_of_result" name="type_of_result" class="form-control">
                        {% for result in study_results %}
                            {% if result != "testid" and result != "studyid"%}
                        <option value="{{ result }}">{{ result }}</option>
                            {% endif %}
                        {% endfor %}
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-md-2 control-label" for="model">Model</label>

                <div class="col-md-3">
                    <select id="platform_models" name="model" class="form-control">
                        {% for model in study_models %}
                        <option value="{{ model }}">{{ model }}</option>
                        {% endfor %}
                    </select>
                </div>
                <label class=" control-label" for="model"></label>

                <div class="col-md-1" style="margin-left: -20px" id="platform_data_form_button">
                    <a href="" class="btn btn-primary btn-success" ></span> Confirm</a>
                </div>
            </div>

        </fieldset>
    </form>
{% endblock %}
Red Shift
  • 1,312
  • 2
  • 17
  • 29

1 Answers1

0

This just seems to be a problem with your Javascript. You're getting the value for selectedResultValue when the page is first loaded, presumably when no value is selected. The Ajax request is made when the button is clicked, but you don't fetch the new value inside that click function: so you still use the old, empty value. Just move that line inside the function.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Yes it mostly fixed it - just have some browser and perl script issues to fix up now. Thanks for your help. – Red Shift Apr 24 '14 at 02:58