Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

7. Python Django connects to the database

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Python Django connects to the database

First, install the database

# yum-y install mysql mysql-devel mysql-server

Check to see if the python database component is installed

# rpm-qa | grep MySQL-python

MySQL-python-1.2.3-0.3.c1.1.el6.x86_64

Third, create a database

# mysql-uroot-p

Mysql > create database csvt character set utf8

Create a project and apply it

# django-admin.py startproject csvt03

# cd csvt03

# django-admin.py startapp blog

# vim settings.py (add applications, modify database type connection users and passwords)

...

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.mysql'

'NAME': 'csvt'

'USER': 'root'

'PASSWORD': '123456'

'HOST':''

'PORT':''

}

}

.

INSTALLED_APPS = (

'django.contrib.auth'

'django.contrib.contenttypes'

'django.contrib.sessions'

'django.contrib.sites'

'django.contrib.messages'

'django.contrib.staticfiles'

'blog'

)

...

5. Create tables

# cat blog/models.py

From django.db import models

Class Employee (models.Model):

Name = models.CharField (max_length=20)

/ / (a name field will be added to the database csvt employee table)

Synchronize the database and write the data to the database

# python manage.py syncdb

Creating tables...

Creating table auth_permission

Creating table auth_group_permissions

Creating table auth_group

Creating table auth_user_user_permissions

Creating table auth_user_groups

Creating table auth_user

Creating table auth_message

Creating table django_content_type

Creating table django_session

Creating table django_site

Creating table blog_employee

You just installed Django's auth system, which means you don't have any superusers defined.

Would you like to create one now? (yes/no): yes

Installing custom SQL...

Installing indexes...

No fixtures found.

7. View the database

Mysql > use csvt

Database changed

Mysql > show tables

+-+

| | Tables_in_csvt |

+-+

| | auth_group |

| | auth_group_permissions |

| | auth_message |

| | auth_permission |

| | auth_user |

| | auth_user_groups |

| | auth_user_user_permissions |

| | blog_employee |

| | django_content_type |

| | django_session |

| | django_site |

+-+

11 rows in set (0.00 sec)

Mysql > desc blog_employee

+-+ +

| | Field | Type | Null | Key | Default | Extra | |

+-+ +

| | id | int (11) | NO | PRI | NULL | auto_increment |

| | name | varchar (20) | NO | | NULL |

+-+ +

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report