django-database [Config]

Source: Internet
Author: User

Os:mac, django1.9.5, python3.5

Database:mysql

0. Background

The Django built-in database is as follows:

Note that regardless of which database server you choose to use, you must download and install the corresponding database adapter

table 1-1. Database Engine Settings
Set Database Adapter
PostgreSQL PostgreSQL PSYCOPG Version 1.x
Postgresql_psycopg2 PostgreSQL PSYCOPG Version 2.x
Mysql Mysql MySQLdb
Sqlite3 Sqlite Python 2.5+ Built-in
Oracle Oracle cx_oracle

1. Installation

We chose MySQL as the database server

Because MYSQLDB does not support Python3.5, you need to install the Pymysql as a Django-operated MySQL adapter.

Of course there will be a corresponding small price, which will be mentioned in the configuration.

Pip Install Pymysql

2. Configuration

The default database in Django is Sqlite3, that is, the initial configuration of the database in the new project post-configuration file (settings.py) is as follows:

1DATABASES = {2     'default': {3         'ENGINE':'Django.db.backends.sqlite3',4         'NAME': Os.path.join (Base_dir,'Db.sqlite3'),5     }6}

2.1 Creating a Database

First, log in to MySQL and create a database mall

Create Database default CharSet=UTF8;

2.2 Creating a user for a database

Then, assign all permissions for the database mall to the user Rinka, with the password rinka0414

Grant  All Privileges  on Mall. *  to ' Rinka '@'localhost'by'rinka0414' ;

2.3 Configuration

Finally, in Django, change the configuration to:

1DATABASES = {2     'default': {3         'ENGINE':'Django.db.backends.mysql',4         'NAME':'Mall',5         'USER':'Rinka',6         'PASSWORD':'rinka0414',7         'HOST':'localhost',8         'PORT':'3306',9     }Ten}

which

Database_engine That is, the database servers mentioned in the "background"

database_name tell the database name to Django

Database_user tell Django which user to use to connect to the database

Database_password tell the Django connection user's password

Database_host the database server that tells Django which host to connect to

Database_port tell Django which port to use when connecting to the database

2.4 Small Price

The last step , remember to add the following in the __init.py__ folder under the project name :

1 Import Pymysql 2 pymysql.install_as_mysqldb ()

Otherwise the error will be:

' MySQLdb '

3. Running

Python manage.py runserver

Can successfully run the project without error, it means the database is successfully connected.

Summarize

Database Configuration in Django:

1. Select a database server

2. Download and install the appropriate adapter

3. Create a Database

4. Configuring the database in a Django configuration file

django-database [Config]

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.