Rendering different types of content

We need to provide a way to render each type of content. Edit the models.py file of the courses application and add the following render() method to the ItemBase model:

from django.template.loader import render_to_string
from django.utils.safestring import mark_safe

class ItemBase(models.Model):
# ...

def render(self):
return render_to_string('courses/content/{}.html'.format(
self._meta.model_name), {'item': self})

This method uses the render_to_string() function for rendering a template and returning the rendered content as a string. Each kind of content is rendered using a template named after the content model. We use self._meta.model_name to generate the appropriate template name for each content model dynamically. The render() method provides a common interface for rendering diverse content.

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

content/
text.html
file.html
image.html
video.html

Edit the courses/content/text.html template and write this code:

{{ item.content|linebreaks|safe }}

Edit the courses/content/file.html template and add the following:

<p><a href="{{ item.file.url }}" class="button">Download file</a></p>

Edit the courses/content/image.html template and write:

<p><img src="{{ item.file.url }}"></p>

For files uploaded with ImageField and FileField to work, we need to set up our project to serve media files with the development server. Edit the settings.py file of your project and add the following code to it:

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

Remember that MEDIA_URL is the base URL to serve uploaded media files and MEDIA_ROOT is the local path where the files are located.

Edit the main urls.py file of your project and add the following imports:

from django.conf import settings
from django.conf.urls.static import static

Then, write the following lines at the end of the file:

if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)

Your project is now ready to upload and serve media files. The Django development server will be in charge of serving the media files during development (that is, when the DEBUG setting is set to True). Remember that the development server is not suitable for production use. You will learn how to set up a production environment in Chapter 13Going Live.

We also have to create a template for rendering Video objects. We will use django-embed-video for embedding video content. django-embed-video is a third-party Django application that allows you to embed videos in your templates, from sources such as YouTube or Vimeo, by simply providing the video's public URL.

Install the package with the following command:

pip install django-embed-video==1.1.2

Edit the settings.py file of your project and add the app to the INSTALLED_APPS, setting as follows:

INSTALLED_APPS = [
# ...
'embed_video',
]

You can find django-embed-video application's documentation at https://django-embed-video.readthedocs.io/en/latest/.

Edit the courses/content/video.html template and write the following code:

{% load embed_video_tags %}
{% video item.url "small" %}

Now run the development server and access http://127.0.0.1:8000/course/mine/ in your browser.

Access the site with a user that belongs to the Instructors group, and add multiple contents to a course. To include video content, you can just copy any YouTube URL, such as https://www.youtube.com/watch?v=bgV39DlmZ2U, and include it in the url field of the form.

After adding contents to the course open http://127.0.0.1:8000/, click the course and click on the ENROLL NOW button. You should be enrolled in the course and redirected to the student_course_detail URL. The following screenshot shows a sample course content:

Great! You have created a common interface for rendering different types of course contents.

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

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