2个相似的视图和模板,但只有一个模板显示表单选项

时间:2018-09-30 18:37:11

标签: django django-templates django-views

我真的很喜欢这个。我有一个工作视图/模板,其中有一个表单选择选项,可填充模型中的选项

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>

现在,我为相同的模板和目的复制了很多此代码:

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>        

但是页面源没​​有显示表单选择的任何选项:

<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>

我实际上已经复制了从第一个视图/模板到第二个视图/模板的所有内容。我想知道是否存在范围问题,def pair重用了def random中的代码,但是我注释掉了def random,但这没有帮助。

1 个答案:

答案 0 :(得分:1)

您的第二个视图不会将上下文传递到render()调用中,因此没有classroom变量,并且模板中没有要迭代的内容。

(请注意,在两个视图中,template = loader.get_template(...)调用均无关紧要,并且没有使用;您应该删除这些行。)