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