Django-database [configuration], django-Database Configuration

Source: Internet
Author: User

Django-database [configuration], django-Database Configuration

OS: mac, django1.9.5, python3.5

Database: mysql

 

 

0. Background

The built-in django database is as follows:

You must download and install the corresponding database adapter no matter which database server you choose to use.

 

Table 1-1. database engine settings
Set Database Adapter
Postgresql PostgreSQL PsycopgVersion 1.x
Postgresql_psycopg2 PostgreSQL PsycopgVersion 2.x
Mysql MySQL MySQLdb 
Sqlite3 SQLite Python 2.5 + Built-in
Oracle Oracle Cx_Oracle 

 

 

1. Install

We chose mysql as the database server.

Because MySQLdb does not support Python3.5, you need to install pymysql as an adapter for django to operate mysql.

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

pip install pymysql

 

 

2. Configuration

In django, the default database is sqlite3, that is, the initial configuration of the database in the configuration file (settings. py) after the project is created is as follows:

1 DATABASES = {2     'default': {3         'ENGINE': 'django.db.backends.sqlite3',4         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),5     }6 }

 

2.1 create a database

First, log on to mysql and create the database mall.

create database mall default charset=utf8;

 

2.2 create a user for the database

Then, assign all the permissions of the database mall to the user rinka with the password rinka0414.

grant all privileges on mall.* to 'rinka'@'localhost' identified by 'rinka0414';

 

2.3 Configuration

Finally, change the configuration in django:

 1 DATABASES = { 2     'default': { 3         'ENGINE': 'django.db.backends.mysql', 4         'NAME': 'mall', 5         'USER': 'rinka', 6         'PASSWORD': 'rinka0414', 7         'HOST': 'localhost', 8         'PORT': '3306', 9     }10 }

 

Where:

DATABASE_ENGINEThat is, the database servers mentioned in "background"

DATABASE_NAMEInform django of the Database Name

DATABASE_USERTell django which user to connect to the database

DATABASE_PASSWORDTell django the password of the connected user

DATABASE_HOSTTell django which database server is connected

DATABASE_PORTTell django which port is used to connect to the database

 

2.4 Low Cost

In the last step, remember to add:

1 import pymysql2 pymysql.install_as_MySQLdb()

Otherwise, an error is reported:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

 

 

 

3. Run

python manage.py runserver

If no error is reported when the project runs successfully, the database is successfully connected.

 

 

 

 

Summary

 

Database Configuration in django:

 

1. Select a Database Server

 

2. download and install the corresponding Adapter

 

3. Create a database

 

4. Configure the database in the django configuration file

 

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.