Install Python3 and django under Linux and configure mysql as the default server method for django
- 2020-05-12 06:39:43
- OfStack
My operating system is centos 6.5
1 first select which database django will use. The default database of django1.10 is sqlite3. I want to use mysql database, but I need to install the sqlite development kit for the convenience of testing.
yum install mysql mysql-devel
# For testing purposes, we need to install it sqlite-devel package
yum install sqlite-devel
Next, we need to install Python, because Python3 has become the mainstream, so we need to install Python3, go to the official website to download the new version of Python3. The version I downloaded is python 3.5.2
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
Unzip and install
# Unpack the tar package
tar xf Python-3.5.2.tgz
# Enter the unzipped package
cd Python-3.5.2
# Configure installation information , My installation path is /usr/install/python3/
./configure --prefix=/usr/install/python3/
# Compile and install
make && make install
4 configure the PATH environment variable
# in /ect/profile.d/ I'm gonna go under new 1 A file python3.sh
vim /etc/profile.d/python3.sh
# Add the following sentence
export PATH=$PATH:/usr/install/python3/bin/
# Then perform
export PATH=$PATH:/usr/install/python3/bin/
By default, Python3.5.2 has already installed pip, but I want to install the newer version of pip
# download pip The installation program
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
# The installation pip
python3 get-pip.py
6 installed django
pip install Django
Install mysqlclient, mysqlclient is a connector between Python3 and mysql.
pip install mysqlclient
At this point, the installation of Python and django is complete!
How do I configure mysql as the default django database?
1 create a new project
# create 1 called mysite The project of
django-admin startproject mysite
Enter the project and modify the settings configuration file
# Access to the project
cd mysite
# Modify the settings The configuration file
vim mysite/settings.py
# find DATABASES attribute
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # will mysql As a django Default database
'NAME':'mysite', # Configure database name
'USER':'root', # Database user
'PASSWORD':'123456', # The user password
'HOST':'127.0.0.1', # Configure the address of the database service, which defaults to null localhost
'PORT':'3306', # Configure port
}
}
3 django will not create the database for us, we need to create the database manually.
# Start the database service
service mysqld start
# Log in to the database and enter the database command line interface
mysql
# create 1 called mysite Database. settings In the file configuration we defined the database name as mysite
mysql>CREATE DATABASE mysite CHARACTER SET=utf8;
# Exit the database command line interface
mysql> quit
4. Create a new app called polls in the mysite project
[root@bogon mysite]# python3 manage.py startapp polls
5. Modify the polls/ models.py file
#
vim polls/models.py
# The changes are as follows:
from django.db import models
# Create your models here.
class student(models.Model):
name=models.CharField(max_length=24)
school=models.CharField(choices=(('sc01',' The first 1 Middle school '),('sc02',' The first 2 Middle school '),('sc03',' The first 3 Middle school ')),max_length=32)
sfid=models.IntegerField(primary_key=True,unique=True,)
phone=models.IntegerField(blank=True,null=True)
emial=models.EmailField(null=True,blank=True)
def __str__(self):
return self.name
If you want to understand models.CharField (), you can refer to my article: model field in django.
6 configure the INSTALLED_APPS property in the settings file
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'polls.apps.PollsConfig', # Add the bank
]
7 notify django, polls models file has been modified.
python3 manage.py makemigrations poll
8 (this step can be skipped) if we want to know how changes made to polls/ models.py map to the database, we can use the following command:
# Unpack the tar package
tar xf Python-3.5.2.tgz
# Enter the unzipped package
cd Python-3.5.2
# Configure installation information , My installation path is /usr/install/python3/
./configure --prefix=/usr/install/python3/
# Compile and install
make && make install
0
9 maps the changes made to the models file to the database
# Unpack the tar package
tar xf Python-3.5.2.tgz
# Enter the unzipped package
cd Python-3.5.2
# Configure installation information , My installation path is /usr/install/python3/
./configure --prefix=/usr/install/python3/
# Compile and install
make && make install
1
10 (this step can be omitted) if you want to add, delete or change your custom model on the admi interface, you need to modify the admin.py file under app.
# Unpack the tar package
tar xf Python-3.5.2.tgz
# Enter the unzipped package
cd Python-3.5.2
# Configure installation information , My installation path is /usr/install/python3/
./configure --prefix=/usr/install/python3/
# Compile and install
make && make install
2