Django 3 By Example
eBook - ePub

Django 3 By Example

Build powerful and reliable Python web applications from scratch, 3rd Edition

  1. 568 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Django 3 By Example

Build powerful and reliable Python web applications from scratch, 3rd Edition

About this book

Learn Django 3 with four end-to-end web projects

Key Features

  • Learn Django 3 by building real-world web applications from scratch in Python, using coding best practices
  • Integrate other technologies into your application with clear, step-by-step explanations and comprehensive example code
  • Implement advanced functionalities like a full-text search engine, a user activity stream, or a recommendation engine
  • Add real-time features with Django Channels and WebSockets

Book Description

If you want to learn the entire process of developing professional web applications with Python and Django, then this book is for you. In the process of building four professional Django projects, you will learn about Django 3 features, how to solve common web development problems, how to implement best practices, and how to successfully deploy your applications.

In this book, you will build a blog application, a social image bookmarking website, an online shop, and an e-learning platform. Step-by-step guidance will teach you how to integrate popular technologies, enhance your applications with AJAX, create RESTful APIs, and set up a production environment for your Django projects.

By the end of this book, you will have mastered Django 3 by building advanced web applications.

What you will learn

  • Build real-world web applications
  • Learn Django essentials, including models, views, ORM, templates, URLs, forms, and authentication
  • Implement advanced features such as custom model fields, custom template tags, cache, middleware, localization, and more
  • Create complex functionalities, such as AJAX interactions, social authentication, a full-text search engine, a payment system, a CMS, a RESTful API, and more
  • Integrate other technologies, including Redis, Celery, RabbitMQ, PostgreSQL, and Channels, into your projects
  • Deploy Django projects in production using NGINX, uWSGI, and Daphne

Who this book is for

This book is intended for developers with Python knowledge who wish to learn Django in a pragmatic way. Perhaps you are completely new to Django, or you already know a little but you want to get the most out of it. This book will help you to master the most relevant areas of the framework by building practical projects from scratch. You need to have familiarity with programming concepts in order to read this book. Some previous knowledge of HTML and JavaScript is assumed.

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Django 3 By Example by Antonio Melé in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Python. We have over one million books available in our catalogue for you to explore.

Information

9

Extending Your Shop

In the previous chapter, you learned how to integrate a payment gateway into your shop. You also learned how to generate CSV and PDF files.
In this chapter, you will add a coupon system to your shop. You will also learn how internationalization and localization work, and you will build a recommendation engine.
This chapter will cover the following points:
  • Creating a coupon system to apply discounts
  • Adding internationalization to your project
  • Using Rosetta to manage translations
  • Translating models using django-parler
  • Building a product recommendation engine

Creating a coupon system

Many online shops give out coupons to customers that can be redeemed for discounts on their purchases. An online coupon usually consists of a code that is given to users and is valid for a specific time frame.
You are going to create a coupon system for your shop. Your coupons will be valid for customers in a certain time frame. The coupons will not have any limitations in terms of the number of times they can be redeemed, and they will be applied to the total value of the shopping cart. For this functionality, you will need to create a model to store the coupon code, a valid time frame, and the discount to apply.
Create a new application inside the myshop project using the following command:
python manage.py startapp coupons 
Edit the settings.py file of myshop and add the application to the INSTALLED_APPS setting, as follows:
INSTALLED_APPS = [ # ... 'coupons.apps.CouponsConfig', ] 
The new application is now active in your Django project.

Building the coupon model

Let's start by creating the Coupon model. Edit the models.py file of the coupons application and add the following code to it:
from django.db import models from django.core.validators import MinValueValidator, \ MaxValueValidator class Coupon(models.Model): code = models.CharField(max_length=50, unique=True) valid_from = models.DateTimeField() valid_to = models.DateTimeField() discount = models.IntegerField( validators=[MinValueValidator(0), MaxValueValidator(100)]) active = models.BooleanField() def __str__(self): return self.code 
This is the model that you are going to use to store coupons. The Coupon model contains the following fields:
  • code: The code that users have to enter in order to apply the coupon to their purchase.
  • valid_from: The datetime value that indicates when the coupon becomes valid.
  • valid_to: The datetime value that indicates when the coupon becomes invalid.
  • discount: The discount rate to apply (this is a percentage, so it takes values from 0 to 100). You use validators for this field to limit the minimum and maximum accepted values.
  • active: A Boolean that indicates whether the coupon is active.
Run the following command to generate the initial migration for the coupons application:
python manage.py makemigrations 
The output should include the following lines:
Migrations for 'coupons': coupons/migrations/0001_initial.py: - Create model Coupon 
Then, execute the next command to apply migrations:
python manage.py migrate 
You should see an output that includes the following line:
Applying coupons.0001_initial... OK 
The migrations are now applied in the database. Let's add the Coupon model to the administration site. Edit the admin.py file of the coupons application and add the following code ...

Table of contents

  1. Preface
  2. Building a Blog Application
  3. Enhancing Your Blog with Advanced Features
  4. Extending Your Blog Application
  5. Building a Social Website
  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. Building an E-Learning Platform
  12. Rendering and Caching Content
  13. Building an API
  14. Building a Chat Server
  15. Going Live
  16. Other Books You May Enjoy
  17. Index