Jinja did not render text on templates while other text displayed

Hi all,

Following the course on IBM I learned fundametal of Django and whilst I locally created a webpage for demo by adding the hands-on labs. However, I am encountering some issues with Jinja2 when I try to render the text of Lessons that belongs to each course.
E.g. code snippet from Week2 - course-detail.html

    <div class="card">
      <h2>{{ course2.name }}</h2>
      <h5>{{ course2.description }}</h5>
    </div>
<!-- Lessons  -  the text did not display 
even though you can retrieve data of Lesson 
using python3 manage.py  shell,  shell_plus, dbshell, dbshell_plus  -->
    <h2>Lessons:</h2>
    {% for lesson in course2.lesson_set.all %}
    <div class="card">
      <h5>Lesson {{ course2.order }} : {{lesson.title}}</h5>
      <p>{{ lesson.content }}</p>
    </div>
    {% endfor %}

If you run code on your local machine, It looks like that:

Having surfed on the internet, could not find out an answer or hint to resolve these issues. Would be glad to hear an advice, hint or even to get the solution from here.
The full codes are available on this github repo: [GitHub - Kanatbek-AKA/Django-WebSite at development]
I appreciate your contribution.

Kind regards,
Kanatbek

The first thing to verify is that you have instances of Lesson that are related to the instance of course2 that you are rendering on this page.

Hi Ken,

Thanks for your reply. May I know how to get all instances of course2 using jinja2 on templates? Whatever I do use, I got an error Manager isn’t accessible via Course2 instances or it is empty.
This is the Lesson2 model of week2:

class Lesson2(models.Model):
    id = models.BigAutoField(primary_key=True)
    title = models.CharField(max_length=200, default="title")
    order = models.IntegerField(default=0)
    course = models.ForeignKey(Course2, on_delete=models.CASCADE)
    content = models.TextField()

View2.py

# Course_detail.html
def course_details(request, course2_id):
        context2 = {}
        if request.method == "GET":
                try:
                        course2 = Course2.objects.get(pk=course2_id)
                        context2['course2'] = course2           # <--- defined as **course2**
                        return render(request, 'onlinecourse/course_detail.html', context2)
                except Course2.DoesNotExist :
                        raise Http404("No course matches the given id.")

Kind regards,
Kanatbek

Your Model name is Lesson2, and you don’t have the related_name attribute in the ForeignKey field in the model, so that means that the related manager is going to be lesson2_set, not lesson_set.

Also see the docs at Related objects reference | Django documentation | Django

Thanks @KenWhitesell.

I am now really confused about jinja outputs. The main reason is that I used lersson2_set.all after configuring the view2.py to get the expected result. I tried lesson2_set.all on Windows10/64 and Chromebook. However, it did not render the text of lessons as I explained in my question above.
Now, after you indicated to the solution, my windows and chromebook are displaying both results. How It’s possible :thinking:

p.s. all changes I removed from history stored in db & migrations

Kind regards,
Kanatbek