Data is not diplaying on Templates

Hi everyone,

First of all, I would like to note that I am a novice in Django, therefore am turining to ask for advise on a issue I cannot not figure out myself.
I have created multiple apps in a project and want to display course lists on the templates i.e. from the one app on the second app. The page is displaying, but the list of courses did not. For example:

Having seen on screenshots posted above did not provide much information to understand what am asking, here is the link to github repo: GitHub - Kanatbek-AKA/Django-WebSite at development to have a look at the code.

Would be glad if someone can assisst me to resolve the issue am encountering.

Thanks in advance.
Kanatbek

Welcome! You’re more likely to get assistance if you post the relevent view and template here. Few people are interested in going through a complete project to try and find an issue without some idea of where to begin.

If you’re having multiple problems, pick one and we’ll address it first.

When posting code here, enclose it between lines of 3 backtick - ` characters. This means that you’ll have a line of ```, then your code, then another line of ```.

Hi @KenWhitesell,

Thank you very much for your reply. Indeed, I haven’t provided the code I want to resolve. Here are the apps code:

My example app - urls.py looks like:

from django.conf import settings
from django.conf.urls.static import static
from django.urls import path, include
from example import views
from week1 import views1
from week2 import views2
from week3 import views3
from week4 import views4
from week5 import views5

urlpatterns = [
    # Templates to render
    path(route='', view=views.HomePageView.as_view(), name='home'), 
    path(route='week1/', view=views.Week1PageView.as_view(), name='week1'), 
    path(route='week2/', view=views.Week2PageView.as_view(), name='week2'), 
    path(route='week3/', view=views.Week3PageView.as_view(), name='week3'), 
    path(route='week4/', view=views.Week4PageView.as_view(), name='week4'), 
    path(route='week5/', view=views.Week5PageView.as_view(), name='week5'), 
    # Hands-on Labs from week1-5 apps
    # Week 1 
    path(route='handson/', view=views1.index, name='lab1'),
    path(route='exercise1/handson/', view=views1.get_date, name='date'),
    # Week 2 
    path('handson2/', include(('week2.urls', 'week2'), namespace='lab2')), # giving namespace
    #  Week 3
    path('handson3/', include(('week3.urls', 'week3'), namespace="lab3")),
    # Week 4
    path('handson4/', include(('week4.urls', 'week4'), namespace='lab4')),
    # Week 5
    path('handson5/', include(('week5.urls', 'week5'), namespace='lab5')),

] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) \
    + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The templates in the example app have buttons to open hands-on labs on a new window e.g. week2.html

 <button class="btn bg-primary">
              <a
                class="p-3 text-white"
                id="handson-exercise"
                rel="noopener noreferrer"
                target="_blank"
                href="{% url 'lab2:popular_course_list' %}"
                >Dive</a
              >
            </button>
......

The week2 app (it is noted with given namespace lab2 ) - urls.py looks like:

from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
from . import views2

urlpatterns = [
    path(route='week2/', view=views2.popular_course_list, name='popular_course_list'),
    path(route='onlinecourse/course/<int:course_id>/enroll/', view=views2.enroll, name='enroll'),
    path(route='onlinecourse/course/<int:course_id>/', view=views2.course_details, name='course_details'),

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)\
 + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

In the same week2 app defined views e.g. to display available courses list for visitors:

from .models import Course2, Lesson2, Enrollment2
from django.urls import reverse
from django.views import generic
from django.http import Http404

def popular_course_list(request):
	context ={}
	if request.method == "GET" :
		#Using the object model manage to read all course list and sort them by total_enrollment descending
		course_list = Course2.objects.order_by('-total_enrollment')[:10]  
		# Append the course list as an entry of context dictionary
		context['course_list'] = course_list
		# Indicate the html file path with param and context
		return render(request, 'onlinecourse/course_list.html', context)
 
               # below enroll and course_detail functions defined as well... 

And there is a template folder with course_list.html and course_detail.html files to render list of courses e.g. course_list.html:

<!--body -->
  <h2>Popular courses list</h2>    
    <!-- Add your template there -->
    {% if course_list %}
      <ul>
        {% for course in course_list %}
          <div class="container">
            <div class="row">
                <div class="column-33">
              <img src="{{ MEDIA_URL }}/{{ course.image }}" width="360" height="360" />
                </div>
                <div class="column-66">
              <h1 class="xlarge-font"><b>{{ course.name }}</b></h1>
              <p style="color:mediumseagreen;"><b>{{ course.total_enrollment }} enrolled</b></p>
              <p>{{ course.description }}</p>
              <!-- Form to create a course view -->
              <form action="{% url 'lab2/course:enroll' course.id %}" method="post">
                  {% csrf_token %}    
                  <input type="submit" value="Enroll" class="button" />
              </form>
              </div>
            </div>
          </div>
          <hr>
        {% endfor %}
        </ul>
    {% else %}
        <p>No Course are available</p>
    {% endif %}

The page returns successfully without an error, but I cannot get the list of courses on the html file of week2 app, and I need advise to know what I have to rectify in the code to display the list of courses.
Here is the link to the github repo: https://github.com/Kanatbek-AKA/Django-WebSite/tree/development

Thanks in advance.

Kind regards,
Kanatbek

So one of the first things to check is to verify that you actually have data in your model.

Run your Django shell and issue the following query:

Are you getting results?

(You could also print course_list after the query to verify that the view is properly retrieving the data.)

I just run python3 manage.py shell

from week2.models import Course2
Course2.objects.all()

got an emtpy query. That means that the DB does not contain data and {% if course_list %} == False.
Hence I do not get the course list on the html ? :slight_smile:

Kind regards,
Kanatbek

Thanks @KenWhitesell,

I have just now learned Django shell to fix the issue :face_with_peeking_eye:

Kind regards,
Kanatbek

Hi @KenWhitesell,

May I ask you my next question in this thread or should I open a new one for that?
Having solved almost all issues learning Django shell after your advise, I cannot figure out the solution for this one:

It indicates to the nonexisting course3_list.html file in week3 template, however, such file I have not referred in the codes. Scanned through each codes and am stuck with.

Kind regards,
Kanatbek

The most common cause of a template-not-found, if you’re not explicitly referencing a template by that name, is an implicit reference created by one of the Django-provided generic Class-Based Views.

My guess would be that you have a ListView defined referencing the model Course3.

In that way I also created the templates of week 4 app and it works fine. Each templates of week apps have different html file names to evade the conflict by reversing them.

May I know the reason of how it’s possible that generic Class-Based Views of week 4 app do work without an error, but week 3 app does not find the file in templates? I am a bit confused to figure out it.

Kind regards,

Look at the CBVs for week 4, then the views for week 3, and identify the difference.

I could not see any difference. May you indicate it to me, please?

Being observent of details is an important skill to develop as a programmer.

You can either visually examine those two files to see the differences, or use a tool that will compare two files. Either way, this is something you need to be able to do.

Thanks! I noted it in my mind.
I think I am a bit tired to see manually the difference in the code of two files. I am going to check it with a tool and let you know.

Good day @KenWhitesell !

Having looked at the difference of the views of week3 - 4, namely at classes, the vscode select compare feature did not give any difference. It might be that I compared files that you did not mean by CBVs of week 4. Please rectify me if I understood it incorrect. By CBVs of week 4 you meant views and models?

By Using functions in views for week 3 app it resolves the template does not exist error, but in class it persists

Kind regards,
Kanatbek

Specifically, the (Class-Based) view in which you’re getting the error along with it’s counterpart in week 4

To better understand about Django Class generic Views I need to dive into that by creating apps and troubleshooting them.

Last question if you do not mind.

All templates are rendering now with data (strings course name and description) as well signup and sign in are working well. However, when I click on enroll button of week 2 app, the Lessons title, content and order did not display while course name and description returning as expected. Although I have defined lessons in DB as Lesson2 Course2 etc.
For example course_list.html contents rendering lessons according course name:

<div class="card">
      <h2>{{ course.name }}</h2>
      <h5>{{ course.description }}</h5>
    </div>
    <!-- Lessons -->
    <h2>Lessons:</h2>
    {% for lesson in course.lesson_set.all %}
    <div class="card">
      <h5>Lesson {{ lesson.order}} : {{lesson.title }}</h5>
      <p>{{ lesson.content }}</p>
    </div>
    {% endfor %}

Content of views2.py

def course_details(request, course_id):
	context = {}
	if request.method == "GET":
		try:
			course = Course2.objects.get(pk=course_id)
			context['course'] = course
			# use render method to generate HTML page by combining tamplate amd context
			return render(request, 'onlinecourse/course_detail.html', context)
		except Course2.DoesNotExist :
			raise Http404("No course matches the given id.")

What I am missing in the template course_list.html to get lessons? What’s wrong there? As a single app it does render all neccessary data, but in multiple apps it did not.

Bes regards,
Kanatbek

What “enroll” button?

Regarding the template, again, use the shell to check your data.

You’ve got:

as the query.

What does course.lesson_set.all() return in the shell?

It is defined in course_list.html. This template works perfectly.

Correction: the lessons string is not displayed on course_detail.html.

Using Lesson2 in query renders the data, but I, beeing frankly, did not know how to retrieve jinjas in django shell.