Building SPAs with Django and HTML Over the Wire

Book description

Discover how to construct real-time applications with Python and Django without the hassle of learning JavaScript

Key Features

  • Learn to put together an SPA using Python and very little JavaScript
  • Create WebSocket communication between the backend and frontend
  • Build a real-world project with Django using the techniques provided in this book

Book Description

The HTML over WebSockets approach simplifies single-page application (SPA) development and lets you bypass learning a JavaScript rendering framework such as React, Vue, or Angular, moving the logic to Python. This web application development book provides you with all the Django tools you need to simplify your developments with real-time results.

You'll learn state-of-the-art WebSocket techniques to realize real-time applications with minimal reliance on JavaScript. This book will also show you how to create a project with Docker from the ground up, test it, and deploy it on a server. You'll learn how to create a project, add Docker, and discover development libraries, Django channels, and bidirectional communication, and from then, on you'll create real projects of all kinds using HTML over WebSockets as a chat app or a blog with real-time comments. In addition, you'll modernize your development techniques by moving from using an SSR model to creating web pages using WebSockets over HTML. With Django, you'll be able to create SPAs with professional real-time projects where the logic is in Python.

By the end of this Django book, you'll be able to build real-time applications, as well as gaining a solid understanding of WebSockets with Django.

What you will learn

  • Explore real-time site realization
  • Understand the proper use of Django channels
  • Find out how to set up Docker with Django
  • Discover how to use a JavaScript framework such as Stimulus
  • Manage a database asynchronously in Django
  • Bring dynamic rendering logic to the backend

Who this book is for

This book is for developers looking to build applications where they want to bring logic to the backend, learn WebSockets, and not depend on JavaScript heavily to create a single-page application. Basic knowledge of HTML and Python and familiarity with basic web development concepts is expected.

Publisher resources

Download Example Code

Table of contents

  1. Building SPAs with Django and HTML Over the Wire
  2. Foreword
  3. Contributors
  4. About the author
  5. About the reviewer
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  7. Part 1: Getting Started with Python
  8. Chapter 1: Setting up the Virtual Environment
    1. Exploring the software required
      1. Operating system
      2. Code editor
      3. Python
      4. Docker
      5. Git
      6. Browser
    2. Adding dependencies
    3. Configuring the IDE
    4. Installing Django
    5. Creating our project
    6. Summary
  9. Chapter 2: Creating a Django Project around Docker
    1. Technical requirements
    2. Exploring the containers used for building our app
    3. Adding the Django service
      1. Creating a Django launcher
    4. Configuring the databases
    5. Connecting Django to a web server
    6. Adding a fake SMTP mail server
    7. Testing for correct operation
      1. Caddy and Django
      2. MailHog
    8. Summary
  10. Part 2: WebSockets in Django
  11. Chapter 3: Adding WebSockets to Django
    1. Technical requirements
    2. Creating our first page with Daphne
    3. Listening to events with consumers
    4. Sending plain text from the backend
    5. Sending JSON from the backend
      1. Example project – Generating a ticket of random numbers
    6. Receiving JSON in the frontend
      1. Example project – Checking for matching numbers
    7. Rendering HTML in the backend
    8. Summary
  12. Chapter 4: Working with the Database
    1. Technical requirements
    2. Inserting information into the database
    3. Rendering database information
    4. Limiting queries with a pager
    5. Deleting rows from a database
    6. Updating rows in a database
    7. Summary
  13. Chapter 5: Separating Communication in Rooms
    1. Technical requirements
    2. Basic functions for managing Channels
    3. Creating a full Chat
      1. Defining the database
      2. Generating random users
      3. Integrating HTML and styles
      4. Defining CSS styles
      5. Creating the view
      6. Declaring JavaScript to listen to messages or send them
      7. Building a Consumer to manage groups
    4. Summary
  14. Part 3: HTML over WebSockets
  15. Chapter 6: Creating SPAs on the Backends
    1. Technical requirements
    2. Switching between pages
    3. Server-side rendering for each route
    4. Including a browser to achieve dynamic navigation
    5. Changing URLs
    6. Hydrating sections or components
    7. Creating temporary sessions for clients
    8. Avoiding cross-site request forgery (CSRF) with WebSockets
    9. Summary
  16. Chapter 7: Creating a Real-Time Blog Using Only Django
    1. Technical requirements
    2. Creating models for the database
    3. Generating fake articles and comments
    4. Listing of articles
    5. Navigating between articles with pagination
    6. Adding an article search engine
    7. Creating a static page
    8. Moving between pages and generating a browser
    9. Implementing an individual page per article
    10. Adding a list of comments
    11. Adding new comments
    12. Offering an RSS feed
    13. Summary
  17. Part 4: Simplifying the frontend with Stimulus
  18. Chapter 8: Simplifying the Frontend
    1. Technical requirements
    2. Installing and configuring Stimulus
    3. Defining a controller
    4. Managing events with actions
    5. Capturing references with targets
    6. An application that converts text into uppercase letters
    7. Summary
    8. Why subscribe?
  19. Other Books You May Enjoy
    1. Packt is searching for authors like you

Product information

  • Title: Building SPAs with Django and HTML Over the Wire
  • Author(s): Andros Fenollosa
  • Release date: August 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781803240190