Django 5 for the Impatient - Daniel Correa - E-Book

Django 5 for the Impatient E-Book

Daniel Correa

0,0
25,99 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Learning Django can be a challenging and time-consuming activity without the right guidance. With hundreds of tutorials, loads of documentation, and unclear explanations out there, it’s easy to lose sight of what’s most important. This book stands out by teaching you how to use Django in just a few days with a focused approach. In this second edition, you’ll go on a fun, practical, and pragmatic journey to learning full-stack development with Django 5. You’ll start building your first Django app within minutes. As you progress, you’ll learn from concise explanations that will help you get to grips with some of the most important Django features, including URLs, views, templates, models, CSS inclusion, image storage, Django admin panel, and more. You’ll also understand how to design Django MVT (Model-View-Template) architectures and implement them. Additionally, you’ll use Django to develop a movie store application and deploy it to the internet.
By the end of this book, you’ll be able to build and deploy your own Django web applications confidently.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 193

Veröffentlichungsjahr: 2024

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Django 5 for the Impatient

Learn the core concepts of Django to develop Python web applications

Daniel Correa

Greg Lim

Django 5 for the Impatient

Copyright © 2024 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

Group Product Manager:Kaustubh Manglurkar

Publishing Product Manager:Bhavya Rao

Senior Content Development Editor:Feza Shaikh

Technical Editor: Simran Ali

Copy Editor:Safis Editing

Project Coordinator: Aishwarya Mohan

Indexer:Subalakshmi Govindhan

Production Designer: Ponraj Dhandapani

Marketing Coordinators:Anamika Singh

First published: June 2022

Second edition: September 2024

Production reference: 1250724

Published by Packt Publishing Ltd.

Grosvenor House 11 St Paul’s SquareBirmingham B3 1RB, UK

ISBN 978-1-83546-155-6

www.packtpub.com

To my mother, my ultimate hero.

– Daniel Correa

To my awesome wife for taking such good care of our family and children so that I could embark on my writing journey. She and our family are the very reason why I write books like this. Thank you so much, dear.

– Greg Lim

Contributors

About the authors

Daniel Correa is a researcher, software developer, and author of programming books. Holding a PhD in computer science, he also serves as a professor at Universidad EAFIT in Colombia. His interests lie in software architectures, frameworks, web development, and clean code.

I want to thank Greg for inspiring me to write books. Greg is one of the best programming book authors I have known. Thanks to my wife, family, colleagues, and friends for all the support. Finally, thanks to Miguel Sosa for his assistance with code revision, and thanks to the entire Packt team for the meticulous work to publish this book.

Greg Lim is a technologist and the author of several books on programming. He has taught programming in tertiary institutions for many years and places a strong emphasis on learning by doing.

I want to thank Daniel, my talented co-author; without him, this book wouldn’t have been possible. Thanks also to everyone on the Packt team who helped us so much.

About the reviewers

Daniel Mitsuo Siena Hirata is a Brazilian full-stack engineer with over half a decade of experience, always up for solving problems, diving into games, and hunting for great food. He is on a journey through the ever-changing tech world, driven by curiosity and a love for learning new things. With years of Django experience and a solid grip on Python, he always add a creative touch to projects. He loves a good challenge and is always on the lookout for innovative solutions, making him a passionate and dynamic professional.

Nilton Pimentel is a Brazilian Python developer. He has more than 4 years of experience in the field of web development. He has developed many projects for companies using Django.

He is passionate about technology, soccer, games, music, and movies, and is always in search of knowledge and trying to evolve every day. His mission is to solve problems and deliver maximum value in people’s lives using Python.

I’d like to thank my mother (Ivanir Pimentel) and my dog (Bolinha) for all their support and love throughout my career, and all the people who have helped me in some way to get here. THANK YOU!

Table of Contents

Preface

1

Installing Python and Django, and Introducing the Movies Store Application

Technical requirements

Introducing and installing Python

Introducing and installing Django

Creating and running a Django project

Understanding the Movies Store application

Introducing the Django MVT architecture

Summary

2

Understanding the Project Structure and Creating Our First App

Technical requirements

Understanding the project structure

The moviesstore folder

manage.py

db.sqlite3

Creating our first app

Creating a home page

Configuring an URL

Creating an about page

Configuring the about URL

Defining about function

Creating about template

Summary

3

Designing a Base Template

Technical requirements

Creating a base template with Bootstrap

Introducing Bootstrap

Introducing Django template language (DTL)

Creating a base template

Registering the base template

Updating the home page to use the base template

Creating the new index template

Creating a custom CSS file

Storing an image

Serving the static files

Updating the base template to use the custom CSS and load static files

Updating the views index function

Updating the About page to use the base template

Creating the new About template

Storing the about.jpg image

Updating the views about function

Adding a header section

Updating the base template

Storing the logo image

Updating the style.css

Adding a footer section

Updating the base template

Updating the style.css

Summary

4

Creating a Movies App with Dummy Data

Technical requirements

Creating the movies app

Creating the movies app

Adding the movies app to settings

Including the movies URL file in the project-level URL file

Listing movies with dummy data

Configuring the movies URL

Defining the views index function

Creating a movies index template

Listing individual movies

Configuring individual movies URLs

Defining the views show function

Creating a movies show template

Adding individual movie links on the movies page

Adding a link in the base template

Summary

5

Working with Models

Technical requirements

Creating our first model

Creating a Movie model

Installing Pillow

Managing migrations

Applying the default migrations

Creating custom migrations

Applying custom migrations

Accessing the Django admin interface

Creating a superuser

Restoring your superuser password

Accessing the admin panel

Configuring image upload

Serving the stored images

Adding a movie model to the admin panel

Adding the Movie model to the admin panel

Summary

6

Collecting and Displaying Data from the Database

Technical requirements

Removing the movies’ dummy data

Updating the movie listings page

Updating index function

Updating the movies.index template

Adding a custom CSS class

Updating the listing of an individual movie page

Updating show function

Updating the movies.show template

Adding a custom CSS class

Implementing a search movie functionality

Updating the movies.index template

Updating index function

Summary

7

Understanding the Database

Technical requirements

Understanding the database viewer

Customizing the Django admin panel

Ordering movies by name

Allowing searches by name

Switching to a MySQL database

Configuring the MySQL database

Configuring our project to use the MySQL database

Running the migrations

Summary

8

Implementing User Signup and Login

Technical requirements

Creating an accounts app

Creating an accounts app

Adding the accounts app to the settings file

Including the accounts URL file in the project-level URL file

Creating a basic signup page

Configuring a signup URL

Defining the signup function

Creating accounts signup template

Adding the signup link to the base template

Improving the signup page to handle POST actions

Customizing UserCreationForm

Creating CustomUserCreationForm

Updating the signup function to use CustomUserCreationForm

Customizing the way errors are displayed

Creating a login page

Configuring a login URL

Defining login function

Creating accounts login template

Adding the link to the base template

Redirecting a registered user to the login page

Implementing a logout functionality

Configuring a logout URL

Defining the logout function

Adding the link to the base template

Summary

9

Letting Users Create, Read, Update, and Delete Movie Reviews

Technical requirements

Creating a review model

Create the review model

Apply migrations

Add the review model to the admin panel

Creating reviews

Updating the movies.show template

Defining the create_review function

Configuring the create review URL

Reading reviews

Updating the movies.show template

Updating the show function

Updating a review

Updating movies.show template

Creating the movies edit_review template

Defining the edit_review function

Configuring the edit_review URL

Deleting a review

Updating the movies.show template

Defining the delete_review function

Configuring the delete_review URL

Summary

10

Implementing a Shopping Cart System

Technical requirements

Introducing web sessions

HTTP protocol limitations

Web sessions

Django login scenario

Django sessions

Creating a cart app

Adding cart app in settings

Including the cart URL file in the project-level URL file

Adding movies to the cart

Configuring the add_to_cart URL

Defining add_to_cart function

Updating the movies.show template

Listing movies added to the cart

Configuring cart index URL

Defining a utils file

Defining a filter

Defining an index function

Creating the cart.index template

Updating the add_to_cart function

Adding a link in the base template

Removing movies from the cart

Configuring clear URL

Defining clear function

Updating the cart.index template

Summary

11

Implementing Order and Item Models

Technical requirements

Analyzing store invoices

Creating the order model

Creating the Order model

Applying migrations

Adding the order model to the admin panel

Creating the Item model

Creating the Item model

Applying migrations

Adding the item model to the admin panel

Recapping the Movies Store class diagram

Summary

12

Implementing the Purchase and Orders Pages

Technical requirements

Creating the purchase page

Configuring the purchase URL

Defining the purchase function

Updating cart.index template

Creating cart.purchase template

Creating the orders page

Configuring the orders URL

Defining the orders function

Creating accounts.orders template

Adding a link in the base template

Recapping the Movies Store MVT architecture

Summary

13

Deploying the Application to the Cloud

Technical requirements

Managing GitHub and Git

Understanding Git and GitHub

Creating a GitHub repository

Uploading our code to GitHub

Cloning your code onto PythonAnywhere

Configuring virtual environments

Setting up your web app

Configuring static files

Summary

Index

Other Books You May Enjoy

1

Installing Python and Django, and Introducing the Movies Store Application

Welcome to Django 5 for the Impatient! This book focuses on the key tasks and concepts to help you learn and build Django applications quickly. It is designed for those of you who don’t need all the details about Django, except for those that you really need to know. By the end of this book, you will be confident in creating your own Django projects.

So, what’s Django? Django is a free, open-source web framework for building modern Python web applications. Django helps you quickly build web apps by abstracting away many of the repetitive challenges involved in building a website, such as connecting to a database, handling security, enabling user authentication, creating URL routes, displaying content on a page through templates and forms, supporting multiple database backends, and setting up an admin interface.

This reduction in repetitive tasks allows developers to focus on building a web application’s functionality, rather than reinventing the wheel for standard web application functions.

Django is one of the most popular frameworks available and is used by established companies such as Instagram, Pinterest, Mozilla, and National Geographic. It is also easy enough to be used by start-ups and to build personal projects.

There are other popular frameworks, such as Flask in Python and Express in JavaScript (for more information on Express, see Beginning Node.js, Express & MongoDB Development by Greg Lim: https://www.amazon.com/dp/B07TWDNMHJ/). However, these frameworks only provide the minimum required functionality for a simple web page, and developers have to do more foundational work, such as installing and configuring third-party packages on their own for basic website functionality.

In this chapter, we are going to get acquainted with the application we are going to build, using Django 5, and get ready to develop our project by installing and setting up everything we need. By the end of the chapter, you will have successfully created your development environment.

In this chapter, we will be covering the following topics:

Introducing and installing PythonIntroducing and installing DjangoCreating and running a Django projectUnderstanding the Movies Store applicationIntroducing Django MVT architecture

Technical requirements

In this chapter, we will use Python 3.10+.

The code for this chapter is located at https://github.com/PacktPublishing/Django-5-for-the-Impatient-Second-Edition/tree/main/Chapter01/moviesstore.

The CiA video for this chapter can be found at https://packt.link/ygUpr

Introducing and installing Python

Python is a high-level programming language (https://www.python.org/), created in the late 1980s by Guido van Rossum. The name Python comes from the creator’s affection for the British comedy group Monty Python and not the “snake,” as is commonly believed.

Python has an open-source license, meaning that developers can modify, use, and redistribute its code for free without paying the original author.

Python is characterized as a friendly and easy-to-learn programming language. Python can be used to develop a wide range of applications, including web development, data analysis, artificial intelligence, scientific computing, and automation.

For now, let’s check whether we have Python installed and, if so, what version we have.

If you are using a Mac, open your Terminal. If you are using Windows, open Command Prompt. For convenience, we will refer to both the Terminal and Command Prompt as Terminal throughout the book.

We will need to check whether we have at least Python 3.10 in order to use Django 5. To do so, go to your Terminal and run the following commands:

For macOS, run this:python3 --versionFor Windows, run this:python --version

This shows the version of Python you have installed. Make sure that the version is at least 3.10. If it isn’t, get the latest version of Python by going to https://www.python.org/downloads/ and installing the version for your OS. For Windows, you must select the Add python.exe to PATH option (to ensure that the Python interpreter can be accessed from any directory in the command prompt or Terminal), as shown in Figure 1.1:

Figure 1.1 – Installing Python on Windows

After the installation, run the command again to check the version of Python installed.

The output should reflect the latest version of Python, such as Python 3.12.2 (at the time of writing), as shown in Figure 1.2:

Figure 1.2 – Checking the Python version on Windows

Now that we have Python installed, let’s move on to introducing and installing Django.

Introducing and installing Django

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design (https://www.djangoproject.com/). Django makes it easier to build better web apps more quickly and with less code.

There are several ways to install Django; we will use pip to install Django in this book. pip is the standard package manager for Python to install and manage packages not part of the standard Python library. pip is automatically installed if you downloaded Python from https://www.python.org/.

First, check whether you have pip installed by going to the Terminal and running the following commands:

For macOS, run this:pip3For Windows, run this:pip

If you have pip installed, the output should display a list of pip commands, as shown in Figure 1.3:

Figure 1.3 – Checking whether pip is installed on Windows

Next, to install Django, run the following commands:

For macOS, run this:pip3 install django==5.0For Windows, run this:pip install django==5.0

The preceding command