Home > Backend Development > Python Tutorial > Detailed introduction to database settings in django (code example)

Detailed introduction to database settings in django (code example)

不言
Release: 2019-01-28 10:43:59
forward
2295 people have browsed it

This article brings you a detailed introduction (code example) about database settings in Django. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

  1. First define the table name and fields of the database

  2. Start the mysql database bash mysql.server start

  3. Install pymysql pip install pymysql

PyMySQL is a library used to connect to the MySQL server in the Python3.x version, and mysqldb is used in Python2.

  1. Add the following code to the _init_.py file:

##import pymysql

pymysql.install_as_MySQLdb()

  1. New database

    mysql -u root -p Log in root
    show databases Show database
    create database mysite Create database

  2. settings.py Configure the database:

1) Django uses the sqlite database by default

2) Modify the DATABASES option of the settings.py file to configure the database
3) Set what you want to use database. For example, mysql

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'mysite',
    'USER': 'root',
    'PASSWORD': '12345678',
    'HOST': 'localhost',
    'PORT': '3306',
}
}
Copy after login
  1. Create app

You can create multiple apps in one project

Use the command python manage.py startapp myApp, and then generate the myApp directory and related files.
admin.py Site configuration
models.py Define model
views.py Define view

  1. Activate app

1) In the settings.py file, add the app name myApp application to INSTALLED_APPS

INSTALLED_APPS = [

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myApp',
Copy after login
]

2) Define the model, one database table corresponds to one model.
Add something similar to the following in models.py:
class Grades(models.Model):

gname = models.CharField(max_length=20)
gdate = models.DateTimeField()
ggirlnum = models.IntegerField()
gboynum = models.IntegerField()
isDelete = models.BooleanField(default=False)
Copy after login
ps: There is no need to define the primary key, it will be automatically generated later.

3) Survive the database table in the database

python manage.py makemigrations
python manage.py migrate

In this way, the database table corresponding to models is generated in the database. But the contents of the table are empty.

The above is the detailed content of Detailed introduction to database settings in django (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template