Install Python3 Django under Linux and configure MySQL as the Django default database (reprint)

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 is Sqlite3, I want to use MySQL database, but in order to test convenient by the way to install the SQLite development package.

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

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

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

3 Unzip and install

#  Unzip the tar package tar XF python-3.5.2.tgz #  into the unpacked package CD python-3.5.2#  configuration installation information, my installation path is/usr/install/python3/./ Configure--prefix=/usr/install/python3/#  compiling and installing make && make install

4 Configuring the PATH environment variable

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

5 By default, Python3.5.2 has Pip installed, 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 Pippython3 get-pip.py

6 Installing Django

Pip Install Django

7 Installing the Mysqlclient,mysqlclient is a Python3 with MySQL connector.

Pip Install Mysqlclient

At this point, Python and Django installation are complete!

How do I configure MySQL for the Django default database?

1 Create a new project

2 Enter the project and modify the settings configuration file

# Enter the PROJECTCD mysite#  Modify settings config file vim mysite/settings.py#  find DATABASES attribute DATABASES = {    ' default ': {        ' ENGINE ': ' Django.db.backends.mysql ',            #  MySQL as django default database        ' name ': ' MySite ',                                 #  Config database name        ' user ': ' Root ',                                   #  database user        ' PASSWORD ': ' 123456 ',                             #  user password        ' HOST ': ' 127.0.0.1 ',                              #  Configure the database service address, if empty then default to localhost        ' port ': ' 3306 ',                                   #  config port    }}

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

#  Start the Database service service mysqld start#  log in to the database and go to the database command line interface mysql#  create a database named MySite. Settings file configuration We defined the database name as Mysitemysql>create mysite CHARACTER Set=utf8;
# exit the database command line interface
Mysql> quit

4 Create a new app named polls in the MySite project

[Email protected] mysite]# Python3 manage.py Startapp Polls

5 Modify polls/models.py File

#  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 high School '), (' Sc02 ', ' Secondary 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 models. Charfield () and other methods, you can refer to my article: Django 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 this line]

7 notifies django,polls that the models file has been modified.

Python3 manage.py Makemigrations Poll

8 (This step can be skipped) if we want to know how the changes to the polls/models.py map 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 the custom model for additions and deletions, you need to modify the admin.py file under the app.

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

(This blog is reproduced)

Install Python3 Django under Linux and configure MySQL as the Django default database (reprint)

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.