Django+mysql Study Notes

Source: Internet
Author: User

This period of time in learning Mysql+django knowledge points. Take a note of the pits and experiences of the following learning process.

The tool used is navicat for MySQL

Python 2.7.12

Mysql-python 1.2.3.

First create a new database (Students_info) in the secondary directory, note the user name, password, port ... This database is subsequently docked in settings.py.

Django and database docking operation flow:

1, you have to tell the Django database information, the following framework will automatically go to the database based on this information to access the databases do not worry about you.

The code is as follows:

DATABASES = {
' Default ':
{
' ENGINE ': ' Django.db.backends.mysql ',
' NAME ': ' Students_info ',
' USER ': ' Root ',
' PASSWORD ': ' 123456 ',
' HOST ': ' 127.0.0.1 ',
' PORT ': ' 3306 ',
}
}

The above is the setting in settings.py, yes it is so simple. Engine according to a variety of databases can refer to the official document https://docs.djangoproject.com/en/1.7/ref/settings/#std: setting-databases.

2, define the data model, this work is in the corresponding app (blog) application directory in the model.py completed. Examples are as follows (djangobook example):

Class Publisher (models. Model):    name = models. Charfield (max_length=30)    address = models. Charfield (max_length=50) City    = models. Charfield (max_length=60)    state_province = models. Charfield (max_length=30)    country = models. Charfield (max_length=50)    website = models. Urlfield () class Author (models. Model):    first_name = models. Charfield (max_length=30)    last_name = models. Charfield (max_length=40)    email = models. Emailfield () class book (Models. Model):    title = models. Charfield (max_length=100)    authors = models. Manytomanyfield (Author)    publisher = models. ForeignKey (Publisher)    publication_date = models. Datefield ()

Each class corresponds to a table.

3. The next step is to run Python manage.py makemigrations blog

Then run Python manage.py migrate

If there is a problem with the second run, you can remove the blog/migrates under 0001_initial.py and rerun the above two commands.

4. If no problem, you can see the created table in Navigcat.

Django+mysql Study Notes

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.