English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
My operating system is centos6.5
1 First, choose which database django will use. django1.10The default database is sqlite3, I want to use the mysql database, but for the convenience of testing, I also need to install the sqlite development package.
yum install mysql mysql-devel #For convenience of testing, we need to install the sqlite development package-devel package yum install sqlite-devel
2 Next, we need to install Python because3It has become mainstream, so next we need to install Python3Go to the official website to download Python3the new version. The version I downloaded is python3.5.2
wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
3 Unpack and install
# Unpack the tar package tar xf Python-3.5.2.tgz # Enter the unpacked 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/Create a new file python under the directory3.sh vim /etc/profile.d/python3.sh # Add the following line export PATH=$PATH:/usr/install/python3/bin/ # Then execute export PATH=$PATH:/usr/install/python3/bin/
5 By default, after installing Python3.5.2pip is already installed, but I want to install a newer version of pip
# Download the pip installation program wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py # Install pip python3 get-pip.py
6 Install django
pip install Django
7 Install mysqlclient, mysqlclient is a Python3Connector for mysql.
pip install mysqlclient
So far, Python and django installation is complete!
How to configure mysql as the default database for django?
1 Create a new project
# Create a project named mysite django-admin startproject mysite
2 Enter the project and modify the settings configuration file
# Enter the project cd mysite # Modify the settings configuration file vim mysite/settings.py # Find the DATABASES attribute DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', # Use mysql as the default database for django 'NAME':'mysite', # Configure the database name 'USER':'root', # Database user 'PASSWORD':'123456' # User password 'HOST':'127.0.0.1' # Configure the address where the database service is located, if empty, it defaults to localhost 'PORT':'3306# Configure the 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 a database named mysite. In the settings 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 named polls in the mysite project
[root@bogon mysite]# python3 manage.py startapp polls
5 Modify polls/models.py file
# vim polls/models.py # Modify 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','First Middle School'),('sc02','Second Middle School'),('sc03','Third 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 methods like models.CharField(), you can refer to my article: 'django's model field'.
6 Configure the INSTALLED_APPS attribute 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 this line ]
7 Notify Django that the models file in polls has been modified.
python3 manage.py makemigrations poll
8 (This step can be skipped) If we want to know what changes have been made to the polls/How to operate the mapping of modifications made to models.py to the database, you can use the following command:
python3 manage.py sqlmigrate polls 0001
9 Map the modifications made to the models file to the database
python manage.py migrate
10 (This step can be omitted) If you want to perform CRUD operations on custom models in the admin interface, you need to modify the admin.py file under the app.
from .models import student # Register the student model admin.site.register(student)
That's all the installation of Python under Linux that the editor has brought to you3This is the full content of how to configure MySQL as the default server for Django and configure it with django, hoping everyone will support and cheer for the tutorial~