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_stringfrom django.utils.safestring import mark_safeclass 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 ...

Get Django 2 by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.