程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django learning diary 1

編輯:Python

Create database

The database configuration in the project is in  bysms/settings.py  in , here

# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
python manage.py migrate # Create table , Each with models.py and migrations Of app Will create a table 

 

Connect to database

I use the   navicat  Connect to database

Create and modify your own database tables

1. Create an application

2. stay models.py in utilize   orm Create a table and add fields , for example 、

from django.db import models
class Customer(models.Model):
# Customer name
name = models.CharField(max_length=200)
# contact number
phonenumber = models.CharField(max_length=200)
# Address
address = models.CharField(max_length=200)

3. stay setting.py Lieutenant general app register , Make the project aware of app And pay attention to the addition, deletion, modification and query of its tables .


INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Join the following line
'common.apps.CommonConfig',
]

4. Execute the command after adding

python manage.py makemigrations common      # Generate the command to update the table

python manage.py migrate      #  Update table  

  Create super admin ( developer )

be used for , You can modify the table

 1.  Create super admin

python manage.py createsuperuser

2. stay   127.0.0.1/admin You can operate the project's own table in

3. Add self created table Code can be found in admin Show

from django.contrib import admin
from .models import Customer
admin.site.register(Customer)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved