Django uses MySQL (Local environment)

Source: Internet
Author: User
Tags sqlite

The Django version used in this blog post is 1.4.0 (recently using SAE, no way, had to withdraw from 1.7.1). But the principle is probably similar.

Django supports a variety of database management tools such as MySQL, PostgreSQL, SQLite, and Oracle. Where SQLite is built into Python, so if you use SQLite, you do not have to configure the configuration, the full use of Python to manage, of course, do not have to look at the Ben Boven.

This blog post takes MySQL as an example to demonstrate the "local environment (corresponding to the cloud environment), Django uses MySQL steps" (First make sure you have MySQL installed)

Step 1:

Modify the settings.py file in your Django project directory to resemble the following:

where ' name ' corresponds to ' db_name ' is the name of the database you created in advance using MySQL's command line prompt. Note: Before Django uses the database , you must first create the database, or you will get an error. ' User ' corresponds to ' username ' and ' PASSWORD ' corresponding to ' passwd ' is the username and password you created in MySQL . If you have more than one, just fill in a few. Both ' HOST ' and ' PORT ' can be blank by default.


Off -topic: Log in to MySQL using your username and password:

first, you need to enter the Mysql/bin directory, or you can set environment variables in. Bash_profile:

then enter passwd.


Step 2:

Then, using the Python manage.py syncdb in the manage.py path, the result will prompt you to find MYSQLDB This module incorrectly, why , because python manage.py syncdb This is how the command works:

1. Locate the app that may need to be updated in the settings.py Installed_apps tuple in the project directory.

2. Locate the models.py (relationship definition file) in each app directory and update it in the database for changes.

Said so much, the front of the error can not find the module mysqldb what is the meaning ah?


Since defining a relationship in models.py is using Python, and actually working in the database to form the model, it is certainly necessary to use SQL statements, so there must be some functional modules to translate the Python statements into SQL statements. If you use SQLite, since both SQLite and conversion modules are already built into Python, there is no error in using direct usage. But "MySQL statement conversion module" requires you to manually load, these modules are placed in the Mysql-python.

I am using pip to install:

Once installed, then use python manage.py syncdb to be OK.

The system I am using is OS X, the following is the default installation path for MySQL

/usr/local/cellar/mysql/5.6.22/bin/mysql_config


Django uses MySQL (Local environment)

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.