Django uses MySQL (Local environment)

Source: Internet
Author: User


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), the steps of Django using MySQL"

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 go into the Mysql/bin directory, You can also set environment variables in. Bash_profile:

PATH=/usr/local/bin:/usr/bin:/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/Local /cellar/mysql/5.6.22/bin/

Then enter mysql-u in Prompt username-p

then enter passwd.


Then, try the Python manage.py syncdb in the manage.py path, and the result will prompt you for the error mysqldb this module, why , because python The manage.py syncdb command works like this:

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?

Give a picture first, then explain:


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/


If you want to know where your database file is placed, you can view the Ldata variable in the Mysql_config file, which is the default path to the database folder store. In my system, the full path to Mysql_config is:

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


Django uses MySQL (Local environment)

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.