25,99 €
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:
Seitenzahl: 193
Veröffentlichungsjahr: 2024
Django 5 for the Impatient
Learn the core concepts of Django to develop Python web applications
Daniel Correa
Greg Lim
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
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.
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!
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 architectureIn 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
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 --versionThis 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.
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:pipIf 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.0The preceding command