Install Python3 and Django under Linux and configure MySQL as the Django default server method _linux

Source: Internet
Author: User
Tags sqlite install django pip install django

My operating system is centos6.5.

1 First choose what database Django will use. django1.10 default database for Sqlite3, I would like to use the MySQL database, but in order to test the convenient way to install the SQLite development package.

Yum install MySQL mysql-devel
#为了测试方便, we need to install the Sqlite-devel package

2 The next need to install Python, because Python3 has become mainstream, so next we have to install Python3, to the official website to download the new version of Python3. I download the version for python3.5.2

wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

3 Decompression and Installation

# Extract TAR packets
tar XF python-3.5.2.tgz 
# into the unpacked package
CD Python-3.5.2
# Configure installation information, my installation path is/usr/install/python3/
./configure--prefix=/usr/install/python3/
# Compiles and installs make
&& make install

4 Configuring the PATH environment variable

# Create a new file under the/ect/profile.d/file python3.sh
vim/etc/profile.d/python3.sh
# Add the following phrase
export path= $PATH:/usr/ install/python3/bin/
#然后执行
export path= $PATH:/usr/install/python3/bin/

5 The Python3.5.2 is installed by default, but I want to install a newer version of PIP

# download PIP installer
wget--no-check-certificate https://bootstrap.pypa.io/get-pip.py
# install PIP
python3 get-pip.py

6 Installing Django

Pip Install Django

The 7 installation mysqlclient,mysqlclient is a Python3 connector with MySQL.

Pip Install Mysqlclient

At this point, the Python and Django Setup is complete!

How do I configure MySQL as the Django default database?

1 Create a new project

# Create a project named MySite

2 Go to this project and modify the settings configuration file

# Enter the project
CD mysite
# Modify Settings profile
vim mysite/settings.py
# Find DATABASES attribute
DATABASES = {
  ' Default ': {
    ' ENGINE ': ' Django.db.backends.mysql ',      # will MySQL as the Django default database
    ' NAME ': ' MySite ',                 # Metabase name
    ' user ': ' Root ',                  # Database user
    ' PASSWORD ': ' 123456 ',               # user password
    ' HOST ': ' 127.0.0.1 ',               # Configure the database service address, if NULL, default to localhost
    ' port ': ' 3306 ',                  # Configure Port
  }
}

3 Django does not create a database for us, we need to create the database manually.

# Start Database Services service
mysqld start
# Login to database and enter database command line interface
MySQL
# Create a database named MySite. In the settings file configuration we defined the database name as MySite
mysql>create db mysite CHARACTER Set=utf8;
# Exit 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 Modifying polls/models.py files

# 
Vim polls/models.py 
# Modified 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 school '), (' Sc02 ', ' Secondary school '), (' sc03 ', ' third secondary 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 () and other methods, you can refer to my article: Django in the Model field.

6 Configuring 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 line
]

7 Notice that the models file in Django,polls has been modified.

Python3 manage.py Makemigrations Poll

8 (Skip this step) if we want to know how the modifications to polls/models.py are mapped to the database, you can use the following command:

Python3 manage.py sqlmigrate Polls 0001

9 mapping changes made to the models file to the database

Python manage.py Migrate

10 (This step can be omitted) if you want to admi in the face of a custom model to check and delete changes, need to be in the app under the admin.py file to modify.

From. Models import Student
# Registration of the student model
Admin.site.register (student)

The above is a small set for everyone to install the Linux Python3 and Django and configure MySQL as the Django Default server method All content, I hope we support cloud-Habitat Community ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.