Database Configuration Guide in the Python Django framework

Source: Internet
Author: User
This article mainly introduces the database configuration guide in the Python Django framework. the example of Python built-in SQLite is given in this article. if you need a friend, you can refer to these concepts and remember them, let's start exploring the Django database layer. First, we need to do some initial configuration; we need to tell Django what database to use and how to connect to the database.

Assume that you have installed and activated the DATABASE server and created a DATABASE in it (for example, use the create database statement ). If you use SQLite, you do not need to install it in this step, because SQLite uses an independent file on the file system to store data.

Like the TEMPLATE_DIRS mentioned in the previous chapter, the database configuration is also in the Django configuration file. the default value is settings. py. Open this file and find the database configuration:

DATABASE_ENGINE = ''DATABASE_NAME = ''DATABASE_USER = ''DATABASE_PASSWORD = ''DATABASE_HOST = ''DATABASE_PORT = ''

The configuration outline is as follows.

DATABASE_ENGINE tells Django which database engine to use. If you use a database in Django, DATABASE_ENGINE must be the value listed in the table.



You must download and install the corresponding database adapter no matter which database server you choose to use. The links in the "required adapters" column in Access Table 5-1 can be obtained free of charge over the Internet. If you use Linux, your release package management system will provide the appropriate packages. For example, find the package of ''python-postgresql ''or ''python-psycopg.

Configuration example:

DATABASE_ENGINE = 'postgresql_psycopg2'

DATABASE_NAME informs Django of the database name. For example:

DATABASE_NAME = 'mydb'

If SQLite is used, specify the complete file system path for the database file. For example:

DATABASE_NAME = '/home/django/mydata.db'

In this example, we place the SQLite database in the/home/django Directory. you can select the most suitable directory.

DATABASE_USER tells Django which user is used to connect to the database. For example, if SQLite is used, it can be blank.

DATABASE_PASSWORD tells Django the password of the connected user. SQLite uses an empty password.

DATABASE_HOST tells Django which host is connected to the database server. If the database and Django are installed on the same computer (that is, the local machine), leave this item blank. Leave this item blank if you use SQLite.

MySQL here is a special case. If MySQL is used and the setting value starts with a slash ('/'), MySQL uses a Unix socket to connect to the specified socket. for example:

DATABASE_HOST = '/var/run/mysql'

Once you enter those settings and save them, you should test your configuration. Run the ''python manage. py shell'' mentioned in the previous chapter in the ''mysite'' project directory for testing. (As mentioned in the previous chapter, the ''manager. py shell'' command is a method for correctly configuring Django to enable the Python interactive interpreter. This method is necessary here, because Django needs to know which configuration file to load to obtain database connection information .)

Run the following commands to test your database configuration:

>>> from django.db import connection>>> cursor = connection.cursor()

If no error message is displayed, your database configuration is correct. Otherwise, you must check the error information to correct the error. The following table shows some common errors.

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.