Because Django is a Python web framework, we first have to install Python.
由于 Django 是一个 Python 网络框架,我们首先要安装 Python。
Installing Python
安装Python
Let ’ s check if we have Python installed and what version it is.
让我们检查一下是否安装了 Python 以及它的版本。
If you are using a Mac, open your Terminal. If you are using Windows, open your Command Prompt. For convenience, I will address both the Terminal and Command Prompt as ‘ Terminal ’ throughout the book.
如果使用 Mac,请打开终端。如果使用的是 Windows,则打开命令提示符。为方便起见,我在本书中将终端和命令提示符都称为 "终端"。
We will need to check if we have at least Python 3.8 to use Django 4. To do so, go to your Terminal and run:
我们需要检查是否至少有 Python 3.8 才能使用 Django 4:
python3 --version
This shows the version of Python you installed. Make sure that the version is at least 3.8. If it is not so, get the latest version of Python by going to python.org. Go to ‘ Downloads ’ and install the version for your OS.
这将显示您安装的 Python 版本。请确保版本至少为 3.8。如果不是,请访问 python.org,获取 Python 的最新版本。转到 "Downloads"(下载)并安装适合您操作系统的版本。
After the installation, run python3 --version again and it should reflect the latest version of Python e.g. Python 3.10.0 (at time of book ’ s writing).
安装完成后,再次运行 python3 --version,它应该反映出 Python 的最新版本,例如 Python 3.10.0(本书编写时)。
Installing Django
安装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 是 Python 的标准软件包管理器,用于安装和管理不属于标准 Python 库的软件包。
First check if you have pip installed by going to the Terminal and running. If you have pip installed, it should display a list of pip commands. To install Django, run the command:
首先检查是否安装了 pip,方法是进入终端并运行。如果已安装 pip,则会显示 pip 命令列表。要安装 Django,请运行以下命令:
pip install django
This will retrieve the latest Django code and install it in your machine. After installation, close and re-open your Terminal.
这将获取最新的 Django 代码并将其安装到你的机器上。安装完成后,关闭并重新打开终端。
Ensure you have installed Django by running:
确保已安装 Django,请运行:
django
It will show you all the options you can use:
它将显示您可以使用的所有选项:
Along the course of the book, you will progressively be introduced to some of the options. For now, we will use the startproject option to create a new project.
在本书的学习过程中,将逐步向你介绍其中的一些选项。现在,我们将使用 startproject 选项创建一个新项目。
In Terminal, navigate to a location on your computer where you want to store your Django project e.g. Desktop. Create a new folder ‘ todoapp ’ with:
在终端中,导航到计算机上要存储 Django 项目的位置,例如 Desktop。创建一个名为 "todoapp "的新文件夹:
mkdir todoapp
‘ cd ’ to that folder:
cd 文件夹:
cd todoapp
In todoapp, run:
在 todoapp 中,运行
python3 -m django startproject
In our case, we want to name our project ‘ django4_react18_todoapp’ . We run:
在我们的例子中,我们希望将项目命名为 " django4_react18_todoapp "。我们运行
mkdir django4_react18_todoapp
cd todoapp
mkdir api
cd api
python -m venv venv
venv/Scripts/activate
django-admin startproject api .
A ‘django4_react18_todoapp ’ folder will be created. We named the Django project backend because it serves as the backend of the Django-React todo app stack. Later on, the React frontend will be contained in a frontend folder. The eventual structure will look something like:
将创建一个 "django4_react18_todoapp "文件夹。我们将 Django 项目命名为后台,因为它是 Django-React todo 应用程序栈的后台。之后,React 前端将包含在 frontend 文件夹中。最终结构如下
In the next chapter, we will look inside the backend folder that Django has created for us and understand it better.
下一章,我们将深入了解 Django 为我们创建的后台文件夹。
领取专属 10元无门槛券
私享最新 技术干货