user1919
user1919

Reputation: 3938

Pass dropdown values from django template to view

I have a django view which renders a template with two dropdown menus:

<div class="input-group" id="_layer" style="display:none;">
              <div class="input-group-btn" >
                  <div class="btn-group btn-group-sm" style="width:100%">
                      <div class="dropdown">
                        <button class="btn btn-primary dropdown-toggle" id="button-countries" type="button" name="name1" data-toggle="dropdown" data-target="#" style="width:100%">Country
                            <span class="caret"></span></button>
                            <ul class="dropdown-menu dropdown-menu-countries" id="country_list" name="name1" style="height: auto; max-height: 200px; overflow-x: hidden;">
                                {% if countries %}
                                    {% for cntr in countries %}
                                          <li><a href="#" id= {{ cntr }}>{{ cntr }}</a></li>
                                    {% endfor %}
                                {% endif %}

                            </ul>
                      </div>
                  </div>
              </div>
              </br>
              <select multiple="multiple" class="has-popover form-control" id="_all_values" data-container="body" data-content="keyword identifies a location Hold down &quot;Control&quot;, or &quot;Command&quot; on a Mac, to select more than one." data-html="true" data-placement="right" name="regions" data-original-title="" title="">
              </select>

              </br>
              <a href="{% url "layer_upload" %}" class="btn btn-primary pull-right">{% trans "Download as Excel" %}</a>

  </div>

The values between the tags are automatically filled, depending on the selection of the first menu.

What I want to do now is to pass the selections of the two dropdown menus (countries and regions) to another view when the user press the button: Download as excel.

What is the most appropriate way to do this in Django?

Upvotes: 0

Views: 1304

Answers (1)

Vincent Loy
Vincent Loy

Reputation: 83

The most simple way to do this (for me) is to submit your information via POST or GET in a basic HTML form. Then, catch the choosen country and region in a dedicated form view.

Another way is to update the link URL on select change (with jQuery, AngularJS or not) if you make your URL like /your-url/the_country_id/the_region_ud. jQuery reference.

HTML example

<select name="test" id="">
  <option value="france">france</option>
  <option value="mexico" selected>mexico</option>
  <option value="china">china</option>
</select>

<a href="https://google.com#q=" target=_blank>Go to <span class="text"></span></a>

Javascript example

var $btn = $('a'),
    $btnHref = $btn.attr('href'),
    $btnTxt = $('.text'),
    $select = $('select');

$btnTxt.text($select.val());
$btn.attr('href', $btnHref + $select.val());

$select.change(function () {
    $btnTxt.text($select.val());
  $btn.attr('href', $btnHref + $select.val());
});

JsFiddle Example

Upvotes: 2

Related Questions