Django interacts with MySQL

Source: Internet
Author: User

See all the software that Ubuntu has installed:

Dpkg-l Dpkg-l | grep MySQL


View the path to the software installation

Dpkg-l | grep MySQL


Check the boot-up software to install additional plugins:

sudo apt-get install rcconfrcconf a whole bit more: sudo apt-get install sysv-rc-confsysv-rc-conf


Install MySQL:

# apt-get Install python-setuptools libmysqld-dev Libmysqlclient-dev # easy_install Mysql-python or #pip install MYSQL-PYT Hon



Django Setting Configuration:

DATABASES = {' default ': {' ENGINE ': ' Django.db.backends.mysql ', ' NAME ': ' Books ', #你的数据库名称 ' US  ER ': ' Root ', #你的数据库用户名 ' PASSWORD ': ', #你的数据库密码 ' HOST ': ', #你的数据库主机, leave blank default to localhost ' PORT ': ' 3306 ', #你的数据库端口}}


Add the following table statement in the Model module:

From django.db import models# Create your models Here.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) county=models. Charfield (default= "CN", 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 (Blank=true) class book (Models. Model): Title=models. Charfield (max_length=100) authors=models. Manytomanyfield (author) publisher=models. ForeignKey (publisher) Publication_date=models. Datefield ()

Django will automatically change these to SQL statements.



Python manage.py validate #测试上面的语句是否有问题.

Python manage.py sqlall app #把上面的内容生成sql语句.

Run the above command error: Commanderror:app ' App ' has migrations. Only the sqlmigrate and Sqlflush commands can is used when a app has migrations. FIX: Delete the Migrations folder in the app.


Python manage.py syncdb #把这些语句写入数据库中.

#选yes建立数据库后台管理的账号和密码.

#如果这步跳过了, the execution:

Python manage.py createsuperuser #创建用户python manage.py ChangePassword #更改密码



#练习在python交互模式下操作数据库:

./manage.py Shell #进入django变量的交互器from app.models Import publisher #导入publisher数据库.


#插入一条数据:

P1=publisher (name= ' Qinghua University ', address= ' Wudaokou ', city= ' Beijing ', state_province= ' Beijing ', county= ' China ', website= ' www.qinghua.com ')

P1.name #查看插入的name

P1.address #查看插入的address

P1.save () #插入的数据写入数据库中


#更新一条数据:

p1.address= "Qinghualu" P1.save ()



#查看所有的数据

In the models module, the following is added below the build Table statement:

def __unicode__ (self): return self.name,self.address

Then go to the swap window to see all the data:

Publisher.objects.all ()


#查询国家等于中国的一条数据:

Publisher.objects.filter (country= "China")


#查询出来的数据进行更改:

A=publisher.objects.get (name= "Beijing Daxue") a.county= "USA" A.save ()


#高效的更新数据方式, and no save required:

Publisher.objects.filter (id=1). Update (name= "Qingdaodaxue")



#在浏览器中打开后台管理数据库界面:

http://192.168.110.106/admin/


The account is the user name and password created when synchronizing the database, log in.

Create a admin.py file under the app

VI admin.py

From Django.contrib import adminfrom app.models import publisher,author,bookadmin.site.register (publisher) Admin.site.register (author) admin.site.register (book)

When you are finished, reopen the page.



#django中引用bootstrap:

In the setting.py:

media_root= '/root/project/statics/bootstrap/'


In the url.py:

From django.conf Import settings


Django interacts with MySQL

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.