Doug Smith
Doug Smith

Reputation: 580

2 similar views and templates but only one template shows form options

I'm really stuck on this one. I have a working view/template that has a form select option that populates options from my model

views.py

def random(request):
    classroom = Classroom.objects.filter(teacher=request.user).order_by('course_block')
    classblock = request.GET.get('class_block')
    students = Student.objects.all().filter(classroom__course_block=classblock)
    nicknames = [s.nickname for s in students]
    data = serializers.serialize("json", students, fields = ("nickname", "attend"))
    student_names = json.dumps(list(nicknames))
    context = {'students': students}
    context['classroom'] = classroom
    context['student_names'] = student_names
    context['data'] = data
    template = loader.get_template('randomizer/randomize.html')
    print (data)
    return render(request, 'randomizer/randomize.html', context)

ramdomize template

{% extends 'randomizer/base.html' %}
{% load static %}

{% block body %}
   <div id="djangorandom">
   {{ classroom.id }}
      <form action="{% url 'randomizer:random' %}" method="get">
           {% csrf_token %}
           <div class="form-group">
               <select class="form-control" name="class_block">
                   {% for room in classroom %}
                       <option value={{ room.course_block }}>{{ room.get_course_block_display }}</option>
                   {% endfor %}
               </select>
            </div>
           <span><input class="btn btn-default" type="submit" value="Submit"></span>
       </form>
   </div>


Page source returns:

<div class="form-group">
    <select class="form-control" name="class_block">

            <option value=11>Block 1-1</option>

            <option value=13>Block 1-3</option>

            <option value=14>Block 1-4</option>

            <option value=P13>Pair 1-3</option>

    </select>
</div>

Now I've copied a lot of this code for a slightly different template and purpose:

def pair(request):
    classroom = Classroom.objects.filter(teacher=request.user).order_by('course_block')
    classblock = request.GET.get('class_block')
    students = Student.objects.all().filter(classroom__course_block=classblock)
    nicknames = [s.nickname for s in students]
    data = serializers.serialize("json", students, fields = ("nickname", "attend"))
    student_names = json.dumps(list(nicknames))
    context= {'classroom': classroom}
    context['students'] = students
    context['student_names'] = student_names
    context['data'] = data
    template = loader.get_template('randomizer/pairing.html')
    print(data)
    return render(request, 'randomizer/pairing.html')


{% extends 'randomizer/base.html' %}
{% load static %}

{% block body %}
    <div id="djangorandom">
    {{ classroom.id }}
        <form action="{% url 'randomizer:pair' %}" method="get">
          {% csrf_token %}
          <div class="form-group">
            <select class="form-control" name="class_block">
              {% for room in classroom %}
                <option value={{ room.course_block }}>{{ room.get_course_block_display }}</option>
              {% endfor %}
            </select>
          </div>
          <span><input class="btn btn-default" type="submit" value="Submit"></span>
        </form>
    </div>        

But the page source doesn't show any of the options for the form selects:

<form action="/randomizer/pairing/" method="get">
    <input type='hidden' name='csrfmiddlewaretoken' value='ADVUsnTserljrnDvRlmeTPyvjMOzva5xj7t8LSeDmPxnkBUtx4XmfXAI5aRfJky6' />
    <div class="form-group">
        <select class="form-control" name="class_block">

        </select>
    </div>
    <span><input class="btn btn-default" type="submit" value="Submit"></span>
</form>

I've practically copied everything from the first view/template to the second view/template. I wondered if there was a scope issue where def pair re-uses the code from def random, but I commented out def random and that didn't help.

Upvotes: 0

Views: 21

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599796

Your second view doesn't pass the context into the render() call, so there is no classroom variable and nothing to iterate over in the template.

(Note, in both views the template = loader.get_template(...) call is irrelevant and not used; you should remove those lines.)

Upvotes: 1

Related Questions