Django implements music website ⑵

Use the Python Django framework to make a music website, continue to develop on the basis of series 1,

This article is mainly about the development of the background singer table module.

Table of contents

table structure design

Singer table (singer) structure

Create table model

Set image upload path

Create upload file directory

generate table migration

Execute create table

Background management table model

Singer table model operation

Add

edit delete

Optimize singer record list

Summarize


table structure design

Singer table (singer) structure

field

type

note

id

int(11)

singer table id

name

varchar(50)

username

pinyin

varchar(50)

Name pinyin

portrait

varchar(200)

Avatar link

first_letter

varchar(15)

initials

gender

tinyint(2)

Gender 0 female 1 male

birthday

varchar(20)

Birthday

height

int(4)

height (cm)

weight

int(3)

weight(kg)

constellation

varchar(50)

constellation

singe_num

int(11)

Number of singles

album_num

int(11)

number of albums

desc

text(0)

Introduction

addtime

int(11)

add time

updatetime

int(11)

edit time

Create table model

Create a singer table model in models.py under the player project directory.

The content is as follows:

from django.db import models
from datetime import date, datetime


# Create your models here.
class Singler(models.Model):
    """ 歌手表模型 """

    name = models.CharField(max_length=50, help_text='请输入歌手名称')
    first_letter = models.CharField(max_length=15, help_text='请输入歌手名称首字母')
    # 设置上传位置
    portrait = models.ImageField(upload_to='uploads/%Y%m%d%H/', help_text='请上传歌手照片')
    birthday = models.DateField(default=date.today, help_text='请选择歌手生日')
    height = models.IntegerField(help_text='请输入歌手身高(cm)', default=0, blank=True)
    weight = models.IntegerField(help_text='请输入歌手体重(kg)', default=0, blank=True)
    constellation = models.CharField(max_length=50, help_text='请输入歌手星座')
    singe_num = models.IntegerField(default=0)
    album_num = models.IntegerField(default=0)
    desc = models.TextField(help_text='请输入歌手简介')
    addtime = models.DateTimeField(auto_now_add=True)
    updatetime = models.DateTimeField(auto_now=True)

Set image upload path

Set at the bottom of myMusic/settings.py.

# 设置文件上传位置
MEDIA_ROOT = 'static/'

Create upload file directory

Create a static folder under the myMusic directory.

As shown in the picture:

generate table migration

python manage.py makemigrations

Implementation process:

Execute create table

python manage.py migrate

Create a table structure as follows:

CREATE TABLE `player_singler` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `first_letter` varchar(15) NOT NULL,
  `portrait` varchar(100) NOT NULL,
  `birthday` date NOT NULL,
  `height` int(11) NOT NULL,
  `weight` int(11) NOT NULL,
  `constellation` varchar(50) NOT NULL,
  `singe_num` int(11) NOT NULL,
  `album_num` int(11) NOT NULL,
  `desc` longtext NOT NULL,
  `addtime` datetime(6) NOT NULL,
  `updatetime` datetime(6) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;

Background management table model

Register the singer table model to the background in admin.py under the player directory.

from django.contrib import admin
from .models import Singler


# Register your models here.
class SinglerAdmin(admin.ModelAdmin):
    pass


admin.site.register(Singler, SinglerAdmin)

Singer table model operation

Add

Click the Add button next to the table model to enter the Add interface and add a new singer record.

After writing, click the save button at the bottom; after the addition is successful, you can view the new record in the list.

edit delete

Through the table model data list, click to enter the singer data details.

 Singer data details can be edited, or click the delete button below to delete the data.

 

Optimize singer record list

Modify player/admin.py.

from django.contrib import admin
from .models import Singler


# Register your models here.
class SinglerAdmin(admin.ModelAdmin):

    # 列表页属性
    def get_name(self):
        return self.name
    get_name.short_description = '歌手名称'

    def get_portrait(self):
        return self.portrait
    get_portrait.short_description = '歌手头像'

    def get_constellation(self):
        return self.constellation
    get_constellation.short_description = '星座'

    def get_height(self):
        return str(self.height) + 'cm'
    get_height.short_description = '身高'

    def get_weight(self):
        return str(self.weight) + 'kg'
    get_weight.short_description = '体重'

    def get_addtime(self):
        return self.addtime
    get_addtime.short_description = '创建时间'

    def get_updatetime(self):
        return self.updatetime
    get_updatetime.short_description = '更新时间'

    # 显示字段
    list_display = ['id', get_name, get_portrait, get_constellation, get_height, get_weight, get_addtime, get_updatetime]
    # 过滤器
    list_filter = ['name', 'constellation']
    # 搜索
    search_fields = ['name', 'constellation']
    # 分页
    list_per_page = 5


admin.site.register(Singler, SinglerAdmin)

 Effect:

Summarize

In django, the sequence of operations from the data table to the background is:

Create table model -> Create table migration file -> Execute table migration -> Background registration.

Optimization is background custom management;

Complicating matters is that all table manipulation modifications require the creation and execution of table migrations.

おすすめ

転載: blog.csdn.net/json_ligege/article/details/132046645