Chapter 1: Installing Python and Django
Welcome to Django 4 for the Impatient! This book focuses on the key tasks and concepts to help you to learn and build Django applications fast. It is designed for readers who don't need all the details about Django except for concepts that you really need to know. By the end of this book, you will be confident 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...
Technical requirements
In this chapter, we will be using Python 3.8+ and pip.
The code for this chapter is located at https://github.com/PacktPublishing/Django-4-for-the-Impatient/tree/main/Chapter01/moviereviews.
Understanding the app we will be building
For our project, we will be building a movie reviews app that will allow users to view and search for movies, as shown in Figure 1.1:
Figure 1.1 – A home page with search functionality
Users will also be able to log in and post reviews of any movies they may have watched, as shown in Figure 1.2:
Figure 1.2 – A movie page listing reviews
They will be able to type in and add their review, as shown in Figure 1.3:
Figure 1.3 – An interface for writing a review
Users can see the list of reviews on a movie's page and post, edit, or delete their own review if they are logged in. They will not be able to edit or delete other users' reviews though. Through building this app, we will learn a lot of concepts, such as forms, user authorization, permissions, foreign keys, and more.
Let's begin by installing Python and Django.
Installing Python
First, 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, I will refer to both Terminal and Command Prompt as Terminal throughout the book.
We will need to check whether we have at least Python 3.8 in order to use Django 4. To do so, go to your Terminal and run the following commands.
For macOS, run this:
python3 --version
For Windows, run this:
python --version
This shows the version of Python you have installed. Make sure that the version is at least 3.8. 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 3.* to PATH option, as shown in Figure 1.4:
Figure 1.4 – Install Python on Windows
After the installation, run the command again to check the...
Installing Django
We will be using pip to install Django. 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.
For macOS, run this:
pip3
For Windows, run this:
pip
If you have pip installed, the output should display a list of pip commands. To install Django, run the following command:
For macOS, run this:
pip3 install Django==4.0
For Windows, run this:
pip install Django==4.0
The preceding command will retrieve the latest Django code and install it on your machine. After installation, close and reopen your Terminal.
Ensure you have installed Django by running the following commands.
For macOS, run this:
python3 -m django
For Windows, run this:
python -m django
Now, the output will show...
Running the Django local web server
In the Terminal, cd
into the created folder:
cd moviereviews
Then, run the following.
For macOS, run this:
python3 manage.py runserver
For Windows, run this:
python manage.py runserver
When you do so, you start the local web server on your machine (for local development purposes). There will be a URL link: http://127.0.0.1:8000/
(equivalent to http://localhost:8000
). Open the link in a browser and you will see the default landing page, as shown in Figure 1.6:
Figure 1.6 – The landing page of the Django project
This means that your local web server is running and serving the landing page. Sometimes, you will need to stop the server in order to run other Python commands. To stop the local server, press Ctrl + C in the Terminal.
Summary
In this chapter, you learned how to install and use Python, pip, and Django. You also learned how to create a new Django project and run a Django local web server. In the next chapter, we will look inside the project folder that Django has created for us to understand it better.