Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain select option value after submit in Django

I am not using Ajax or django forms. I have plain HTML select tag inside the form tag and sending the selected option value in my view. I want to retain the select option value after the submission. Currently, it comes to default value of 1 irrespective of my selection. Help would be appreciated.

Here is my template code:

            <form method = "post" action = "{% url 'index' %}">
                {% csrf_token %}
                <label class="mainlbl">Vega</label>
                <select name = "drop1" >
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                </select>
                <input class="btn btn-ocean btn-side-bar" type = "submit" value="Submit">
            </form>

Here is my view for that:

def index(request):
    if request.method == "POST":
        vega = request.POST['drop1']
        vega = int(vega)
        gvo = GVOptimize('NSE', 'Nifty')
        data = gvo.get_optimal_strategies(vega)
        str1 = None

        for i in range(0, len(data)):
            if i == 0:
                str1 = data[i]

            elif i == 1:
                str2 = data[i]

            elif i == 2:
                str3 = data[i]

            elif i == 3:
                str4 = data[i]

            else:
                break
        context_dict = {'str1': str1, 'str2': str2, 'str3': str3, 'str4': str4 'vega': vega}
        return render(request, 'demo/dashboard.html', context_dict)
    else:
        context_dict = {}
        return render(request, 'demo/dashboard.html', context_dict)
like image 354
Raman Balyan Avatar asked Sep 12 '25 09:09

Raman Balyan


1 Answers

If you really insist on having a hacky way of maintaining the selected option you can change your form to check every option to see if it equals the number you pass back in your context

<form method = "post" action = "{% url 'index' %}">
    {% csrf_token %}
    <label class="mainlbl">Vega</label>
    <select name = "drop1" >
        {% for idx in "useaformpls!" %}
            <option value="{{ forloop.counter }}" {% if context_val == forloop.counter %}selected {% endif %}>
                 {{ forloop.counter }}
             </option>
        {% endfor %}
    </select>
    <input class="btn btn-ocean btn-side-bar" type = "submit" value="Submit">
</form>

Where context_val equals the index you pass back in the context data of your view.

like image 200
Sayse Avatar answered Sep 14 '25 01:09

Sayse