Database Configuration Guide in Python's Django framework

Source: Internet
Author: User
Tags sqlite database
With these ideas in mind, let's start the Django database layer exploration. First, we need to do some initial configuration, we need to tell Django what database to use and how to connect to the database.

We assume that you have completed the installation and activation of the database server and that you have created the database in it (for example, with the Create DB statement). If you are using SQLite, you do not need to install this step because SQLite uses a separate file on the file system to store the data.

Like the template_dirs mentioned in the previous section, the database configuration is also in the Django configuration file, and the default is settings.py. Open this file and look for 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 the database in Django, database_engine must be the value listed in the table.



Note that regardless of which database server you choose to use, you must download and install the corresponding database adapter. Access the links in the "Required Adapters" column in table 5-1 for free access to these adapters via the Internet. If you use Linux, your release package management system will provide the appropriate package. For example, find the package "Python-postgresql" or "PYTHON-PSYCOPG".

Example configuration:

Database_engine = ' POSTGRESQL_PSYCOPG2 '

database_name informs Django of the database name. For example:

database_name = ' MyDB '

If you are using SQLite, specify the full file system path to the database file. For example:

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

In this example, we put the SQLite database in the/home/django directory, you can choose the most suitable for your directory.

Database_user tells Django which user to use to connect to the database. For example: If you use SQLite, blank.

Database_password tells the Django connection user's password. SQLite can use a blank password.

Database_host tells Django which host the database server is connected to. If the database and Django are installed on the same computer (that is, native), you can leave this blank. If you use SQLite, this entry is left blank.

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

Database_host = '/var/run/mysql '

Once you have entered those settings and saved it, you should test your configuration. We can perform the test in the "MySite" project directory under the "Python manage.py Shell" as mentioned in the previous chapter. (as we mentioned in the last chapter, the "manager.py Shell" command is a way to enable the Python interaction interpreter with the correct Django configuration.) This method is necessary here because Django needs to know which configuration file to load to get the database connection information. )

Enter 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 will need to review the error message to correct the error. The following table is a few common errors.

  • 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.