Python's Django uses MySQL basic operations

Source: Internet
Author: User
Tags mysql login

Environment: Centos6.6, python2.6

Preparatory work:

MySQL installation, as well as the installation of Mysql-python http://www.cnblogs.com/zychengzhiit1/p/4437310.html

  

1. Create a Python project

django-admin.py Startproject Pythondemo

2. Enter the created project directory Pythondemo to create the Python app

django-admin.py Startapp Blog

3. Configure settings.py

Changes in several places:

  

Modify:

ENGINE:django.db.backend.mysql (using MySQL database)

Name:demo (to create a good db in MySQL now)

User:root (MySQL login name)

Password:root (MySQL login password)

Host: (Master IP)

Port: (port number)

  

Add the name of the Python app;

4.code-first, importing a model from a class

       

Vim models.py # Modified models.py  from Import Models class Employee (models. Model):        = models. Charfield (MAX_LENGHT=20)  

5. According to the structure of the custom class, synchronize the MySQL table and the structure of the table (Blog_employee table, Name field and primary key ID)

Python manage.py syncdb

After synchronization, look at the MySQL table and the table structure:

  

Table name: Python app name + custom class name

Table structure: Fields for custom classes

At this point, your Django framework's Python application, the data model corresponds to the MySQL table structure;

Python's Django uses MySQL basic operations

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.