James L.
James L.

Reputation: 1153

Django 1.6 Pass dropdown selection value to another template

I'm trying to show the value in a doclistings.html template selected by user from the index.html page. I can't seem to print the value and I'm not getting any errors either. I don't think the value is being stored.

Here is the index page with the dropdown value I'm trying to store.

 <div class="signup">
      <div class="form-group">
        <form action="/doclistings/" method="post">
        <select class="form-control" id="select" name="spec">
          <option><b>Choose a Speciality...</b></option>
          <option value ="Dermatologist">Dermatologist</option>
          <option value = "Dentist">Dentist</option>
          <option value = "ENT">Ear, Nose and Throat (ENT)</option>
          <option value = "Opthalmologist">Eye Doctor</option>
          <option value = "Psychiatrist">Psychiatrist</option>
          <option value = "Orthopedist">Orthopedist</option>
        </select>
        <span class="input-group-btn">
          <button class="btn btn-primary" type="submit"  name="submit" id="ss-submit">Find Doctors</button>
        </span>
      </div>
    </div>

I want the user to select the dropdown value and hit submit. The user should be then directed to the doclistings page with value printed on the top of the page. The submit button does take it to the doclisting but it's not printing the value.

Here is the doclistings.html template where I'm trying to show the value

{{value}}

Here are my views.py

def index(request):
       d = getVariables(request,dictionary={'page_name': "Home"})
       if request.method == "POST":
        form = DropdownSelectionForm(request.POST)

        if form.is_valid():
            selection = form.cleaned_data[{{ form.value }}]
            selection.save()

            return HttpResponseRedirect('/doclistings', {{ form.value }})
    else:
        form = DropdownSelectionForm()

    return render(request, 'meddy1/index.html', {'form': form})

@csrf_exempt
def doclistings(request):
    d = getVariables(request)
    doctors = Doctor.objects.all()
    paginator = Paginator(doctors, 20) #Show 20 doctors per page
    page =  page = request.GET.get('page')
    try:
        doctors = paginator.page(page)
    except PageNotAnInteger:
        doctors = paginator.page(1)
    except EmptyPage:
        doctors = paginator.page(paginator.num_pages)
    d['doctors'] = doctors
    d['paginator'] = paginator
    return render_to_response('meddy1/doclistings.html',d)

urls.py

url(r'^doclistings/$', views.doclistings, name='doclistings'),

forms.py

class DropdownSelectionForm(forms.Form):
    selection = forms.CharField()

Upvotes: 1

Views: 1465

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599530

You have quite a few problems here.

The main one is that you don't include a name attribute in your <select> tag, so your browser won't send the data at all, and request.POST will be empty.

However, you have a form, so you should be using that to display the data in the first place - instead of manually listing the select and its options in HTML, you should just do {{ form.value }}.

But again, you have a problem: you are creating the form variable in the view, but you never send the variable to the template context. You're missing the third parameter to render, which is the dictionary of template variables:

return render(request, 'meddy1/index.html', {'form': form})

You have the same problem in doclistings: you're merrily building up a dictionary, d, of all the variables you want to use in the template, but then never actually pass them to that template. Again, the call should be:

return render(request, 'meddy1/doclistings.html', d)

Upvotes: 1

Related Questions