Accessing the course contents

We need a view for displaying the courses the students are enrolled in, and a view for accessing the actual course contents. Edit the views.py file of the students application and add the following code to it:

from django.views.generic.list import ListView
from courses.models import Course

class StudentCourseListView(LoginRequiredMixin, ListView):
model = Course
template_name = 'students/course/list.html'

def get_queryset(self):
qs = super(StudentCourseListView, self).get_queryset()
return qs.filter(students__in=[self.request.user])

This is the view for students to list the courses they are enrolled in. It inherits from LoginRequiredMixin to make sure that only logged in users can access the view. It also inherits from the generic ListView for displaying a list of Course objects. We override the get_queryset() method for retrieving only the courses the user is enrolled in; we filter the QuerySet by the student's ManyToManyField field for doing so.

Then, add the following code to the views.py file:

from django.views.generic.detail import DetailView

class StudentCourseDetailView(DetailView):
model = Course
template_name = 'students/course/detail.html'

def get_queryset(self):
qs = super(StudentCourseDetailView, self).get_queryset()
return qs.filter(students__in=[self.request.user])

def get_context_data(self, **kwargs):
context = super(StudentCourseDetailView,
self).get_context_data(**kwargs)
# get course object
course = self.get_object()
if 'module_id' in self.kwargs:
# get current module
context['module'] = course.modules.get(
id=self.kwargs['module_id'])
else:
# get first module
context['module'] = course.modules.all()[0]
return context

This is StudentCourseDetailView. We override the get_queryset() method to limit the base QuerySet to courses in which the user is enrolled. We also override the get_context_data() method to set a course module in the context if the module_id URL parameter is given. Otherwise, we set the first module of the course. This way, students will be able to navigate through modules inside a course.

Edit the urls.py file of the students application and add the following URL patterns to it:

path('courses/',
views.StudentCourseListView.as_view(),
name='student_course_list'),

path('course/<pk>/',
views.StudentCourseDetailView.as_view(),
name='student_course_detail'),

path('course/<pk>/<module_id>/',
views.StudentCourseDetailView.as_view(),
name='student_course_detail_module'),

Create the following file structure inside the templates/students/ directory of the students application:

course/
detail.html
list.html

Edit the students/course/list.html template and add the following code to it:

{% extends "base.html" %}

{% block title %}My courses{% endblock %}

{% block content %}
<h1>My courses</h1>

<div class="module">
{% for course in object_list %}
<div class="course-info">
<h3>{{ course.title }}</h3>
<p><a href="{% url "student_course_detail" course.id %}">
Access contents</a></p>
</div>
{% empty %}
<p>
You are not enrolled in any courses yet.
<a href="{% url "course_list" %}">Browse courses</a>
to enroll in a course.
</p>
{% endfor %}
</div>
{% endblock %}

This template displays the courses the user is enrolled in. Remember that when a new student successfully registers with the platform, they will be redirected to the student_course_list URL. Let's also redirect students to this URL when they log in to the platform.

Edit the settings.py file of the educa project and add the following code to it:

from django.urls import reverse_lazy
LOGIN_REDIRECT_URL = reverse_lazy('student_course_list')

This is the setting used by the auth module to redirect the user to after a successful login if no next parameter is present in the request. After successful login, students will be redirected to the student_course_list URL to view the courses that they are enrolled in.

Edit the students/course/detail.html template and add the following code to it:

{% extends "base.html" %}

{% block title %}
{{ object.title }}
{% endblock %}

{% block content %}
<h1>
{{ module.title }}
</h1>
<div class="contents">
<h3>Modules</h3>
<ul id="modules">
{% for m in object.modules.all %}
<li data-id="{{ m.id }}" {% if m == module
%}class="selected"
{% endif %}>
<a href="{% url "student_course_detail_module"
object.id m.id %}">
<span>
Module <span class="order">{{ m.order|add:1 }}
</span>
</span>
<br>
{{ m.title }}
</a>
</li>
{% empty %}
<li>No modules yet.</li>
{% endfor %}
</ul>
</div>
<div class="module">
{% for content in module.contents.all %}
{% with item=content.item %}
<h2>{{ item.title }}</h2>
{{ item.render }}
{% endwith %}
{% endfor %}
</div>
{% endblock %}

This is the template for enrolled students to access the contents of a course. First, we build an HTML list including all course modules and highlighting the current module. Then, we iterate over the current module contents and access each content item to display it using {{ item.render }}. We are going to add the render() method to the content models next. This method will take care of rendering the content properly.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.28.70