Reputation: 769
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
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
{% 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 %}
Upvotes: 0
Views: 116
Reputation: 467
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>
Upvotes: 1