我想用html打印某些属性值。为此,我将这些值附加到views.py中的列表(List),然后通过上下文将其传递到相应的模板(viewperformance.html)。当我在views.py中打印列表时,它会给出一个数字列表,与预期的一样。但是,在模板中打印记录根本没有任何价值。
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 }}
根本没有任何价值,而视图中的列表会打印列表。
答案 0 :(得分:0)
尝试
{% for list_obj in record %}
{{list_obj|safe}}
{% endfor %}
或
{{record|safe}}
希望它可以帮助您解决问题