【问题标题】:Passing list from views.py to template showing null value将列表从views.py传递到显示空值的模板
【发布时间】:2019-04-06 08:19:07
【问题描述】:

我想在 html 中打印某些属性值。为此,我将这些值附加到 views.py 中的列表 (List) 中,并通过上下文将其传递给相应的模板 (viewperformance.html)。当我在views.py 中打印List 时,它会按预期给出一个数字列表。但是,模板中的记录根本没有任何价值。

VIEWS.PY

@csrf_protect
@login_required
def edit_marks(request, course_code):

        # Some code irrelevant to the problem has been removed

        List = list()
        for i in range(len(registered_students)):
            m_id = registered_students[i]            
            s = score[i]

            rows = Marks.objects.filter(mid=m_id, exam_type=exam)
            num = Marks.objects.filter(mid=m_id, exam_type=exam).count()
            record = Marks.objects.filter(mid=m_id, exam_type=exam).values_list('marks', flat=True)

            List.append(list(record))

            if num==0:
                Marks.objects.create(
                    mid=m_id,
                    exam_type=exam,
                    marks=s
                    )
            else:
                Marks.objects.filter(mid=m_id, exam_type=exam).update(marks=s)

        print(List)

        return HttpResponse("Upload successful")  
        context= {'registered_students': registered_students, 'record':List}
    return render(request, 'coursemanagement/viewperformance.html', context)

viewperformance.html

{% load static %} {% block viewperformance %}

<div>

    <form class="ui large form" name="entermarks" id="entermarks" method="POST" action="/ocms/{{curriculum.course_code}}/edit_marks"> 

    {% csrf_token %}

    <select name="examtype" id = "examtype" style="width: 100px">
        <option value = "Quiz 1">Quiz 1</option>
        <option value = "Quiz 2">Quiz 2</option>
        <option value = "Mid Sem">Mid Sem</option>
        <option value = "End Sem">End sem</option>
    </select>

    <table class="ui very basic collapsing celled table">
        <thead>
        <tr>
            <th>Students</th>
        </tr>
        </thead>

        <tbody>
        <p> 'Value of record : ' {{ record }} </p>
        {% for x in registered_students %}
            <tr>
            <td>
                <div class="content">
                <p style="text-align:center">{{x.student_id}}</p>
                </div>
            </td>
            <td>
                <input type="number" name="enteredmarks" id="enteredmarks" required="true">
            </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    <input type="submit" class="ui primary button" id="submit_marks" value="Upload">


    </form>
</div>
{% endblock %}

<script type="text/javascript" src="{% static 'globals/js/jquery.min.js' %}"></script>
<script type="text/javascript">

$(function() {
    $("#entermarks").submit(function(event) {
            event.preventDefault();
            var friendForm = $(this);

            var posting = $.post( friendForm.attr('action'), friendForm.serialize() );
            // if success:
            posting.done(function(data) {
                alert('Upload Successful');
                // window.location = "/academic-procedures/main/";
            });
            // if failure
            posting.fail(function(data) {
                alert('Failed');
                // window.location = "/academic-procedures/main/";
            });
    });
});

</script>

{{ record }} 根本没有给出任何值,而视图中的 List 打印一个列表。

【问题讨论】:

    标签: html django django-views


    【解决方案1】:

    试试这个

    {% for list_obj in record %}
       {{list_obj|safe}}
    {% endfor %}
    

    {{record|safe}}
    

    希望对您的问题有所帮助

    【讨论】:

      猜你喜欢
      • 2014-01-13
      • 2014-12-03
      • 2012-04-07
      • 1970-01-01
      • 2017-02-21
      • 2019-09-21
      • 2021-07-06
      • 2017-02-28
      • 2020-04-23
      相关资源
      最近更新 更多