Django-based video-on-demand website development-step2-building environment

Django-based video-on-demand website development-step2-building environment

In this lecture, I will lead everyone to build a development environment. We will install python, pip, django, mysql and other necessary libraries in sequence.

Install python

Installing Python is very simple. Go to the official Python website to find the download address of Python3. Choose the 32-bit or 64-bit installation package according to your system. After downloading, double-click to install.

After the installation is complete, enter python -v on the command line. If the version number of Python is output, it means that Python has been installed successfully.

$ python3 -V
Python 3.7.0
 

Install pip

If python3 is already installed, then pip3 will be installed automatically.

Install django

Installing django is very simple, with one command.

pip3 install django
 

Install mysql

Since this project uses mysql database, mysql needs to be installed.

If you are using Windows or macOS, you can go to the MySQL official website to download the installation package directly and install it step by step (I installed MySQL 5.7). During the installation process, you will be prompted to create an account and password, so remember to create it~.

If you are using an Ubuntu system, you can use the apt-get command to install

sudo apt-get install mysql-server
 

After the installation is complete, you can use to mysql -Vview the mysql version number.

Then create a database and name it video

CREATE DATABASE video CHARACTER SET utf8;
 

Install PyCharm

PyCharm is a powerful Python editor that is cross-platform. The development of all functions of our project is done on pycharm.

Go to the PyCharm official website to download the PyCharm installation package. Select the version of the corresponding system (Windows/Mac) to download. Generally, it is enough for learning to install the community version directly.

After downloading, double-click and click Next to install.

Other installation

In addition, the following are the class libraries that will be used in the project development process, which are placed in requirements.txt

django_ratelimit==1.1.0  

PyMySQL==0.9.2 ( mysql )

django_chunked_upload==1.1.3  

sorl-thumbnail==12.5.0  

pillow==5.3.0  
 

You can use pip3 to install directly

Such aspip3 install -r requiredments.txt

Create a Django project

Everything is ready, we create a django project, only one line of command is needed

django-admin startproject videoproject
 

After creation, you can use pycharm to open the videoproject folder to view the file structure

Pycharm is very powerful, with its own command line tool (Terminal) and version control tool (Version Control).

Open Terminal, enter

python3 manage.py runserver
 

To start the project, we will often use the command line to debug the program in the subsequent development.

Command line output

Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
 

Then enter http://127.0.0.1:8000/in the browser address bar to see the default homepage of django.

Project configuration

The configuration file of the project is located atvideoproject/videoproject/settings.py

Configuration code

The first thing that needs to be configured is the text encoding format. The default encoding of django is English format. We change it to Chinese format and need to modify the values of the following variables.

LANGUAGE_CODE = 'zh-hans' # zh-hans 
TIME_ZONE = 'Asia/Shanghai' #  
USE_I18N = True
USE_L10N = True
USE_TZ = False
 

Configure static

Then you need to configure the resource file directory to store CSS, Javascript, Images and other files. Here we set the directory to/static/

STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
 

Configuration database

Then you need to configure the database information. Django uses the sqlite database by default, and we modify it to the mysql database. Find the DATABASES node and modify it to the following code. Among them, NAME is the database name, USER is the username of mysql, PASSWORD is the password, HOSY is 127.0.0.1, PORT is 3306

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'video',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST':'127.0.0.1',
        'PORT':'3306',
    }
}
 

After configuring the database, you also need to videoproject/videoproject/__init__.pyinstall the mysql driver, just write the code:

import pymysql
pymysql.install_as_MySQLdb()
 

The prerequisite for the above code to run is that the PyMySQL class library has been installed on your computer.

Finally, you can run the project again to check whether the configuration is correct.