Django5+React18前后端分离开发实战02 安装Python和Django

发布于:2024-05-21 ⋅ 阅读:(101) ⋅ 点赞:(0)

Because Django is a Python web framework, we first have to install Python.

Installing Python

Let’s check if we have Python installed and what version it is.

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.

We will need to check if we have at least Python 3.8 to use Django4.

Todo so, go to your Terminal and run:

python3 --version

注意:这里我选择使用Python3.11 + Django5,如果有不会安装的同学,去看到搭建Anaconda环境的那篇文章。

这里,我们使用Anaconda构建虚拟环境:

# 查看原来的虚拟环境
conda env list

# 删除原来的虚拟环境
conda env remove --name django

# 创建新的虚拟环境
conda create --name django python=3.11.7

# 激活环境
conda activate django

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.

Aflter the installation, run python3 --version again and it should reflect the latest version of Python e.g. Python 3.10.0.

在这里插入图片描述

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.

First check if you have pip install by going to the Terminal and running:

pip

在这里插入图片描述

If you have pip installed, it should display a list of pip commands.

To install django, run the command:

pip install django

在这里插入图片描述

This will retrieve the latest Django code and install it in your machine.

After installation, close and re-open your Terminal.

Ensure you have installed Django by running:

pip list

在这里插入图片描述

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.

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:

mkdir todoapp

cd to that folder:

cd todoapp

In todoapp, run:

django-admin startproject backend

A “backend” 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.

In the next chapter, we will look inside the backend folder that Django has created for us and understand it better.