如何在Django网站中的模板中修复manytomanyfield中的显示数据 [英] How to fix display data in manytomanyfield in my template in Django website
本文介绍了如何在Django网站中的模板中修复manytomanyfield中的显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过将其作为上下文传递来在模板中显示一些数据,但是我得到了'add_students_by_manager'对象是不可迭代的
,我注意到问题实际上与在模板.我该如何解决这个问题.
I am trying to display some data in my templates by passing it as context but i get 'add_students_by_manager' object is not iterable
I noticed that the problem actually has to do with outputting the result on the template. how can i fix this please.
models.py
models.py
class add_courses(models.Model):
Course_Name = models.CharField(max_length=200, blank=True)
student = models.ManyToManyField(add_students_by_manager, blank=True)
def __str__(self):
return self.Course_Name
class add_students_by_manager(models.Model):
manager_ID = models.ForeignKey(Manager_login_information, on_delete=models.CASCADE)
student_ID = models.CharField(max_length=200)
student_name = models.CharField(max_length=200)
def __str__(self):
return self.student_name
views.py
def assignment_page(request):
if request.method == "POST":
get_course_name = request.POST.get('get_course_name')
stu_course_all_stu = add_courses.objects.filter(Course_Name=add_courses_get)
for all_stu_details in stu_course_all_stu:
for stu_details in all_stu_details.student.all():
print(stu_details.student_ID)
# THIS PRINTS OUT ALL THE student_id
context3 = {"stu_course_all_stu": stu_course_all_stu, "stu_details": stu_details}
return render(request, 'assignment_page.html', context3)
else:
return redirect('/')
assignment_page.html
assignment_page.html
{% for m in stu_details %}
<div class="card mb-3 ml-5" style="max-width: 840px;">
<div class="row no-gutters">
<div class="col-md-4">
<img src="/media/uploads/student_user.png" class="rounded profile-pic mt-2 ml-5 mb-2" alt="user" height="120" width="140">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">Student ID : {{m.student_ID}}</h5>
<h5 class="card-title">Student Name : {{m.student_name}}</h5>
<form action="{% url 'stu_id_details' %}" method="POST">{% csrf_token %}
<div class="form-group">
<input type="hidden" name="get_id_stu_details" value="{{m.student_ID}}" class="form-control" id="exampleInputPassword2">
</div>
<button type="submit" class="btn btn-outline-success btn-sm">See Student Details</button>
</form>
</div>
</div>
</div>
</div>
{% endfor %}
推荐答案
because you are not rendering the queryset to template , you are only printing it in your views using a for loop.You can do it in this way
views.py
ls = []
for all_stu_details in stu_course_all_stu:
ls.append(all_stu_details)
context3 = {"stu_course_all_stu": stu_course_all_stu, "stu_details": ls}
return render(request, 'assignment_page.html', context3)
html
{% for m in stu_details.student.all %}
<h5 class="card-title">Student ID : {{m.student_ID}}</h5>
<h5 class="card-title">Student Name : {{m.student_name}}</h5>
这篇关于如何在Django网站中的模板中修复manytomanyfield中的显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文