Django's own background management configuration

Configuration of Django's own background management

Create projects and apps

Modify profile

Database configuration

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',#Select the name of the database. Please confirm that you have this database in mysql
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': 'localhost',
        'PORT': '3306',
        }
}

Application configuration

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'home'
]

Time and language related configuration

LANGUAGE_CODE = 'zh-Hans'   # Default is English

TIME_ZONE = 'Asia/Shanghai'    # time zone

USE_I18N = True

USE_L10N = True

USE_TZ = False       # Date format setting

Create a class under the currently applied models

class Users(models.Model):
    uname=models.CharField(max_length=30)
    email=models.CharField(max_length=50)
    age=models.IntegerField(default=20)
    sex=models.IntegerField()
    addtime=models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.uname

Generate migration file

python manage.py makemigrations

Execution transfer

pythin manage.py migrate

Startup project

Start project access 127.0.0.1:8000/admin / login required

Create administrator user password

Execute python manage.py createsuperuser
Then enter the user name and password as prompted
Then visit 127.0.0.1:8000/admin/
Then enter the account number and password to log in
After success, you need to configure as follows

Configure the admin.py file under the current application

#  Import model
from . import models
class UsersAdmin(admin.ModelAdmin):
  # Fields to show
  list_display = ('id','uname','age','sex','email','addtime')

  #List? Editable set the default editable field
  list_editable = ['uname', 'age','email']

   #Set how many records are displayed per page. The default is 100 records
  list_per_page = 10

  #ordering sets the default sorting field. A negative sign indicates descending sorting
  ordering = ('id',)

  #Filter groups
  list_filter =('uname', 'age', 'email') 

   #Search field
  search_fields =('uname', 'age', 'email') 

  # Detailed time layered filtering
  date_hierarchy = 'addtime'

# Registering device
admin.site.register(models.Users,UsersAdmin)

Tags: Python Django Database MySQL

Posted on Mon, 02 Dec 2019 13:27:25 -0500 by Havery Jay