Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Django 4 By Example - Fourth Edition

You're reading from  Django 4 By Example - Fourth Edition

Product type Book
Published in Aug 2022
Publisher Packt
ISBN-13 9781801813051
Pages 766 pages
Edition 4th Edition
Languages
Author (1):
Antonio Melé Antonio Melé
Profile icon Antonio Melé

Table of Contents (20) Chapters

Preface 1. Building a Blog Application 2. Enhancing Your Blog with Advanced Features 3. Extending Your Blog Application 4. Building a Social Website 5. Implementing Social Authentication 6. Sharing Content on Your Website 7. Tracking User Actions 8. Building an Online Shop 9. Managing Payments and Orders 10. Extending Your Shop 11. Adding Internationalization to Your Shop 12. Building an E-Learning Platform 13. Creating a Content Management System 14. Rendering and Caching Content 15. Building an API 16. Building a Chat Server 17. Going Live 18. Other Books You May Enjoy
19. Index

Building an E-Learning Platform

In the previous chapter, you learned the basics of the internationalization and localization of Django projects. You added internationalization to your online shop project. You learned how to translate Python strings, templates, and models. You also learned how to manage translations, and you created a language selector and added localized fields to your forms.

In this chapter, you will start a new Django project that will consist of an e-learning platform with your own content management system (CMS). Online learning platforms are a great example of applications where you need to provide tools to generate content with flexibility in mind.

In this chapter, you will learn how to:

  • Create models for the CMS
  • Create fixtures for your models and apply them
  • Use model inheritance to create data models for polymorphic content
  • Create custom model fields
  • Order course contents and modules
  • Build authentication views...

Setting up the e-learning project

Your final practical project will be an e-learning platform. First, create a virtual environment for your new project within the env/ directory with the following command:

python -m venv env/educa

If you are using Linux or macOS, run the following command to activate your virtual environment:

source env/educa/bin/activate

If you are using Windows, use the following command instead:

.\env\educa\Scripts\activate

Install Django in your virtual environment with the following command:

pip install Django~=4.1.0

You are going to manage image uploads in your project, so you also need to install Pillow with the following command:

pip install Pillow==9.2.0

Create a new project using the following command:

django-admin startproject educa

Enter the new educa directory and create a new application using the following commands:

cd educa
django-admin startapp courses

Edit the settings.py file of the...

Serving media files

Before creating the models for courses and course contents, we will prepare the project to serve media files. Course instructors will be able to upload media files to course contents using the CMS that we will build. Therefore, we will configure the project to serve media files.

Edit the settings.py file of the project and add the following lines:

MEDIA_URL = 'media/'
MEDIA_ROOT = BASE_DIR / 'media'

This will enable Django to manage file uploads and serve media files. MEDIA_URL is the base URL used to serve the media files uploaded by users. MEDIA_ROOT is the local path where they reside. Paths and URLs for files are built dynamically by prepending the project path or the media URL to them for portability.

Now, edit the main urls.py file of the educa project and modify the code, as follows. New lines are highlighted in bold:

from django.contrib import admin
from django.urls import path
from django.conf import settings
from...

Building the course models

Your e-learning platform will offer courses on various subjects. Each course will be divided into a configurable number of modules, and each module will contain a configurable number of contents. The contents will be of various types: text, files, images, or videos. The following example shows what the data structure of your course catalog will look like:

Subject 1
  Course 1
    Module 1
      Content 1 (image)
      Content 2 (text)
    Module 2
      Content 3 (text)
      Content 4 (file)
      Content 5 (video)
      ...

Let’s build the course models. Edit the models.py file of the courses application and add the following code to it:

from django.db import models
from django.contrib.auth.models import User
class Subject(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=200, unique=True)
    class Meta:
        ordering = ['title']
    def __str__(self):
        return self...

Creating models for polymorphic content

You plan to add different types of content to the course modules, such as text, images, files, and videos. Polymorphism is the provision of a single interface to entities of different types. You need a versatile data model that allows you to store diverse content that is accessible through a single interface. In Chapter 7, Tracking User Actions, you learned about the convenience of using generic relations to create foreign keys that can point to the objects of any model. You are going to create a Content model that represents the modules’ contents and define a generic relation to associate any object with the content object.

Edit the models.py file of the courses application and add the following imports:

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey

Then, add the following code to the end of the file:

class Content(models.Model):
    module...

Adding authentication views

Now that you have created a polymorphic data model, you are going to build a CMS to manage the courses and their contents. The first step is to add an authentication system for the CMS.

Adding an authentication system

You are going to use Django’s authentication framework for users to authenticate to the e-learning platform. Both instructors and students will be instances of Django’s User model, so they will be able to log in to the site using the authentication views of django.contrib.auth.

Edit the main urls.py file of the educa project and include the login and logout views of Django’s authentication framework:

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.auth import views as auth_views
urlpatterns = [
    path('accounts/login/', auth_views.LoginView.as_view(), 
          name='login&apos...

Additional resources

The following resources provide additional information related to the topics covered in this chapter:

Summary

In this chapter, you learned how to use fixtures to provide initial data for models. By using model inheritance, you created a flexible system to manage different types of content for the course modules. You also implemented a custom model field on order objects and created an authentication system for the e-learning platform.

In the next chapter, you will implement the CMS functionality to manage course contents using class-based views. You will use the Django groups and permissions system to restrict access to views, and you will implement formsets to edit the content of courses. You will also create a drag-and-drop functionality to reorder course modules and their content using JavaScript and Django.

Join us on Discord

Read this book alongside other users and the author.

Ask questions, provide solutions to other readers, chat with the author via Ask Me Anything sessions, and much more. Scan the QR code or visit the link to join the book community.

https://packt.link/django

lock icon The rest of the chapter is locked
You have been reading a chapter from
Django 4 By Example - Fourth Edition
Published in: Aug 2022 Publisher: Packt ISBN-13: 9781801813051
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}