J
J
Jekson2019-07-11 15:15:38
Django
Jekson, 2019-07-11 15:15:38

How to pass value from drop down list to view in template?

In the template, so far I have built such a design

<form action="{% url 'cv'  format_='???????' %}" method="post">
               <fieldset>
                <select class="form-control custom-select-main mx-auto" >
                  <option value="json">json</option>
                  <option value="html">html</option>
                  <option value="docx">docx</option>
                </select>
               </fieldset>
</form>
                <a class="btn btn-success" href="{% url 'cv'  format=???????? %}">Get CV</a

Now the page displays a button and a drop-down box with 3 options, but I don’t understand how to transfer the selected option to the view as a format value.
Can you please tell me how to implement this functionality?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jekson, 2019-07-11
@Lepilov

Great way without creating forms

<div id="selector">
    <select>
        <option value="#">Download</option>
        <option value="{% url 'cv'  format_='docx' %}">DOCX</option>
        <option value="{% url 'cv'  format_='json' %}">JSON</option>
        <option value="{% url 'cv'  format_='html' %}">HTML</option>
    </select>
</div>

<script>
    $(function(){
        // bind change event to select
        $('#selector select').bind('change', function () {
            var url = $(this).val(); // get selected value
            if (url) { // require a URL
                window.location = url; // redirect
            }
            return false;
         });
     });
 </script>

A
alternativshik, 2019-07-11
@alternativshik

Assign an action to the form with the required url, add name="format" to the select and put the button into the form. No js is needed here.
Learn to work with forms, and not write crutches, which you will understand later.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question