Python3 + Django3 develops a simple personnel management system

1. Use PyCharm to create a Django project

Python3 + Django3 develops a simple personnel management system

Python3 + Django3 develops a simple personnel management system

Remember to install mysqlclient

(1) Database configuration

We find the DATABASES configuration item in the settings.py file of the project and modify its information to:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',  # 或者使用 mysql.connector.django
        'NAME': 'userinfo',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

Python3 + Django3 develops a simple personnel management system

Chinese comments are added here, so you need to add #-*-coding: UTF-8-*-at the head of the HelloWorld / settings.py file.

The above contains the database name and user information, which are the same as the corresponding database and user settings in MySQL. According to this setting, Django connects to the corresponding database and user in MySQL.

(2) Change the language, time zone and all hosts are accessible

We find and configuration items in the settings.py file of the project ALLOWED_HOSTS, LANGUAGE_CODEand TIME_ZONEmodify their information to:

ALLOWED_HOSTS = ['*']

# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-Hans'

# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'

(3) Modify the TestModel / models.py file:

from django.db import models

# Create your models here
class User(models.Model):
    GENDER_CHOICES = (
        ('男','男'), ('女','女'),
    )
    name = models.CharField(max_length=20, verbose_name='姓名', unique=True)
    birthday = models.DateTimeField(max_length=10,verbose_name='生日', null=True,blank=True)
    gender = models.CharField(max_length=30, choices=GENDER_CHOICES, verbose_name='性别')
    account = models.IntegerField(default=0,verbose_name='工号')
    age = models.IntegerField(default=18, verbose_name='年龄')

The above class name represents the name of the database table, and inherited models.Model. The fields in the class represent the fields in the data table (name), and the data type is defined by CharField (equivalent varchar), DateField(equivalent datetime), and the max_length parameter defines the length.

(4) Create a database

Remember to create in the databaseUserInfo数据库

<1> The database can be created in the cmd command line

G:\四期\python\UserSystem>mysql -uroot -p 
#登陆数据库

mysql> CREATE DATABASE xgp DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
#创建utf8的数据库

<2> Create database in mysql manager

Python3 + Django3 develops a simple personnel management system

Python3 + Django3 develops a simple personnel management system

(5) Create a table structure

$ python manage.py migrate   # 创建表结构
//失败了可用python manage.py migrate UserInfo
//失败了可用python3 manage.py migrate UserInfo

$ python manage.py makemigrations UserInfo  # 让 Django 知道我们在我们的模型有一些变更
$ python manage.py migrate UserInfo   # 创建表结构

The database generates the following table:

Python3 + Django3 develops a simple personnel management system

(6) Visit

Remember to start the project

Python3 + Django3 develops a simple personnel management system

2. Django Admin management tool

Django provides web-based management tools.

The Django automatic management tool is part of django.contrib. You can see it in INSTALLED_APPS in the settings.py of the project:

#/HelloWorld/HelloWorld/settings.py 文件代码:
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'UserInfo.apps.UserinfoConfig',
]

django.contrib is a huge set of features, it is an integral part of Django's basic code.

(1) Activation management tool

Usually we will automatically set it in urls.py when generating the project, we just need to remove the comment.

The configuration items are as follows:

#/HelloWorld/HelloWorld/urls.py 文件代码:
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

When all of this is configured, the Django management tools can be run.

(2) Use management tools

Start the development server, and then visit http://127.0.0.1:8000/admin/ in the browser to get the following interface :

Python3 + Django3 develops a simple personnel management system

Because I am creating a new project now, I need to create a table structure:

$ python manage.py migrate UserInfo   # 创建表结构,指定数据库
//失败了可用python3 manage.py migrate

$ python manage.py makemigrations UserInfo  # 让 Django 知道我们在我们的模型有一些变更
$ python manage.py migrate UserInfo   # 创建表结构

You can create a super user by the command python manage.py createsuperuser , as shown below:

# python manage.py createsuperuser
Username (leave blank to use 'root'): admin
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.

Then enter the user name and password to log in, the interface is as follows:

Python3 + Django3 develops a simple personnel management system

(3) In order for the admin interface to manage a data model, we need to register the data model to admin.

from django.contrib import admin
from . models import User

class HostAdmin(admin.ModelAdmin):
    list_display = [
        'name', 'birthday', 'gender', 'account', 'age',
    ]
    search_fields = ('name',)
admin.site.register(User,HostAdmin)
admin.AdminSite.site_header = '运维系统管理后台'
admin.AdminSite.site_title = '运维系统'

Browser visit

Python3 + Django3 develops a simple personnel management system

Python3 + Django3 develops a simple personnel management system

Set up two employee information and check

Python3 + Django3 develops a simple personnel management system

The userinfo_user table in the userinfo database will store employee information:

Python3 + Django3 develops a simple personnel management system

Guess you like

Origin blog.51cto.com/14320361/2488448