Django uses mysql (in a local environment)

Source: Internet
Author: User

Django uses mysql (in a local environment)

The django version used in this example is 1.4.0 (sae was recently used, so I had to withdraw it 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. Sqlite is built in python. Therefore, if sqlite is used, you do not need to configure it and use python to manage it. Of course, you do not need to read this blog post.

This blog uses mysql as an example to demonstrate how to use mysql in a local environment (corresponding to a cloud environment) (first, make sure that you have installed mysql)

 

Step 1:

Modify the settings. py file in the django project directory as follows:

 

The 'db _ name' corresponding to 'name' is the NAME of the database you created at the command line prompt of mysql. Note: You must create a database before using the database in django. Otherwise, an error is returned. The 'username' corresponding to 'USER' and 'Password' corresponding to 'passwd' are the username and PASSWORD you created in mysql. If you have more than one, just enter one. Either 'host' or 'Port' can be left blank by default.

Question: How to log on to mysql using the user name and password:

First, you need to enter the mysql/bin directory, or you can set the environment variable in. bash_profile:

 

PATH =/usr/local/bin:/usr/bin:/sbin:/usr/local/sbin: /usr/local/Cellar/mysql/5.6.22/bin/

Enter mysql-u username-p in prompt, press enter, and then enter passwd.

Step 2:

Then, in the manage. use python manage in The py path. if you try py syncdb, the system will prompt you that the module MySQLdb cannot be found by mistake. Why? Because python manage. the py syncdb command works like this:

1. Find the APP that may need to be updated in the INSTALLED_APPS tuples of settings. py in the project directory.

2. Find models. py (relational definition file) in each APP directory and update the changes in the database.

After talking about this, the previous error cannot find module MySQLdb. What does it mean?

 

Give a picture first and explain it again:

Because in models. python is used to define the relationship in py, and the actual operation to form a model in the database must use SQL statements. Therefore, some functional modules must be provided to convert python statements into SQL statements. If you use sqlite, since sqlite and conversion modules are already built in python, direct use will not cause errors. However, you need to manually load the "mysql statement conversion module", which is placed in MySQL-python.

I installed it using pip:

After installation, use python manage. py syncdb.

 

The system I use 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 stored, you can view the ldata variable in the mysql_config file. The value of this variable is the path of the default database folder. In my system, the complete path of mysql_config is:

 

/Usr/local/Cellar/mysql/5.6.22/bin/mysql_config

 

 

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.