Use Django's choices options and rich text editor

Project Preparation

1. Create a database

create database choices_test default charset utf8;

2. Create a Django project called choices_test of;

3. Create application app01;

Project structure shown below

 4. configuration database;

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'choices_test',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

5.__init__.py

import pymysql

pymysql.install_as_MySQLdb()

 

Use choices of options

Use the options in the model class choices

1. Preparation of Goods class model

from django.db Import Models 

class Goods (models.Model): 
    STATUS_CHOICES = ( 
        (0, ' off the shelf ' ), 
        ( 1, ' shelves ' ), 
    ) 
    ststus = models.SmallIntegerField (= STATUS_CHOICES choices, default = 1, the verbose_name = " product was added off the shelf " ) 

    class Meta: 
        db_table = " goods "   # specify the name of the table to create a table named goods 
        verbose_name = " commodity "
        verbose_name_plural = verbose_name   # and above in combination with a display table called "commodity" in the background management

verbose_name role

  • verbose_name: Set the name displayed in the admin, and by default will later add s, represents a complex;
  • verbose_name_plural = verbose_name; ---> s admin provided without a display;

 

2. Goods to file registration admin.py model class 

from django.contrib import admin
from app01.models import Goods


admin.site.register(Goods)

 

Table 3. Data Migration

python manage.py makemigrations

python manage.py migrate

 

4. Configure Admin

Configuring time zone

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

Create a super administrator

python manage.py createsuperuser
user:admin
password:admin123

 

5. Start project

Login Admin

The following page

 choices option renderings

 

 

Rich Text Editor

Rich text editor ready

1. Setup Editor package

pip install django-tinymce

2. Registration Application tinymce

INSTALLED_APPS = [
    'tinymce',
]

3. Add tinymce configuration settings file

# Editor Related 
TINYMCE_DEFAULT_CONFIG = {
     ' Theme ' : ' advanced ' ,
     ' width ' : 600 ,
     ' height ' : 400 , 
}

4. Configure Route tinymce

from django.conf.urls import url, include

urlpatterns = [
    url(r'^tinymce/', include('tinymce.urls')),
]

 

Model using the editor classes

1. Preparation model class code

from tinymce.models Import HTMLField 


class Goods (models.Model):
     "" " Goods category Test Model " "" 
    STATUS_CHOICES = ( 
        (0, ' off the shelf ' ), 
        ( 1, ' shelves ' ), 
    ) 
    ststus = models.SmallIntegerField (choices = STATUS_CHOICES, default =. 1, the verbose_name = " product was added to the shelf " )
     # null = True allows the database is empty, blank = True allows the background when input is null 
    detail = HTMLField (null = True, blank = True, verbose_name = " product details ")

    classMeta: 
        db_table = " Goods "   # specify the name of the table to create a table named Goods 
        verbose_name = " commodity " 
        verbose_name_plural = verbose_name   # and use a combination of the above, the display table called "commodity" in the background management

 

Table 2. Data Migration

python manage.py makemigrations

python manage.py migrate

 

3. Go to Admin, increase merchandise

At this point, we can see the following page in the background Management

 

 

 That success using the rich text editor in the background.

 

Guess you like

Origin www.cnblogs.com/yifchan/p/python-1-35.html