Tommy Gibbons
Tommy Gibbons

Reputation: 326

Passing multiple variables from dropdowns using Django/Python

I'm learning Python. I would like display results based on multiple selections from dropdowns.

The following code is working and returning the correct results for one dropdown. I would like to add at least one more dropdown, hopefully multiple, but I cannot fathom how to achieve passing and returning the multiple vars.

Form code

        <p> <form method="get" action="">

            <select name="search3">
                {% for cs in course_list %}
                    <option value="{{ cs.pk }}">{{ cs.name }}</option>
                {% endfor %}
            </select>
            <br/><br/>

            <input type=submit value="Find staff" />
            </form>
    </p>



    {% if employee_list %}
        {{ employee_list.count }} employees matching : <strong>{{ course }}</strong><br/><br/>
        {% for e in employee_list %}
        {% if not e.img1 %}
            <img src="/media/images/null_image.png" width="35"  title="{{ e.first_name}} {{e.surname }}" />
        {% else %}
            <img src="{{ e.img1.url }}" width="50" title="{{ e.first_name}} {{e.surname }}"/>
        {% endif %}
        <a href="/employee/{{ e.slug }}" target="_blank" rel="popup" title="Details...<br />
            {% for det in employee_list.employeedynamic_set.all %}
            {{ det.mobile }}<br />
            {{ det.depot }}<br />
            {% endfor %}
        ">&nbsp;{{ e.first_name }}&nbsp;{{ e.surname }}</a><br/>
        {% endfor %}
    {% else %}
        <p>No employees matched: <strong>{{ course }}</strong></p>
    {% endif %}

views.py code

#   function to list training courses and participating uindividuals.
def CourseView(request):

    course_list     = Course.objects.all().order_by('name')
    if 'search3' in request.GET:
        search3     = request.GET['search3']
        course      = Course.objects.get(pk=search3)
    else:
        search3 = None
        course = None

    return render_to_response("course_display.html", {'course':course,  'course_list': course_list, 'search3': search3 })

Is it possible to add an another dropdown option or even multiple dropdown options and get a result. Or am I barking up the wrong tree?

UPDATE.

In order to pass one or multiple variables back to my function the code is simply the following

def CompetencyCheck(request):
    course_list = Course.objects.all()
    if 'search3' in request.GET:
        search3 = request.GET.getlist('search3')
        course = Course.objects.filter(id__in=search3).distinct().order_by()

.get() allows only the last variable passed from the web page form to be read/passed back to the function. On the other hand, the .getlist() allows one or multiple vars to be passed back. Great, part the of problem solved.

The list of names being returned is unique in that if var1 returns 4 names and var2 returns 8 names and both vars have one common name, a list of 11 names in returned.

I want only the common names to both vars returned, i.e. 1 name to be returned.

Any ideas?

Upvotes: 1

Views: 1742

Answers (2)

Pradeep
Pradeep

Reputation: 139

you can query using course_list only. for example:

course_list     = Course.objects.all().order_by('name')
var1 = request.POST.get('var1')
if var1:
    course_list = course_list.objects.filter(some = var1)
var2 = request.POST.get('var2')
if var2:
    course_list = course_list.objects.filter(some = var2)

Upvotes: 0

GChorn
GChorn

Reputation: 1317

request.GET in your view is a dictionary; by doing request.GET['search3'] you're just accessing the value of its search3 key. So in your template, you should be able to just add other <select> elements with their own names and retrieve their submitted values from request.GET in the same way.

Upvotes: 1

Related Questions