1. First, we download the DjangoUeditor package. Click the link below to download! Download and unzip to the project root.
2. Register the APP in settings.py, and add 'DjangoUeditor' in installed APUs,.
myblog/settings.y INSTALLED_APPS = [ 'django.contrib.admin', .... 'blog', 'DjangoUeditor', #Register APP app ]
3. Add url to myblog/urls.py.
myblog/urls.py from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('blog.urls')), path('ueditor/', include('DjangoUeditor.urls')), ]
4. Create a database model. Write code in blog/models.py.
from django.db import models from django.contrib.auth.models import User from DjangoUeditor.models import UEditorField #Add this line of code in the header to import UEditorField # Article classification class Category(models.Model): name = models.CharField('Blog categories', max_length=100) index = models.IntegerField(default=999, verbose_name='Sort by category') class Meta: verbose_name = 'Blog categories' verbose_name_plural = verbose_name def __str__(self): return self.name #Article tag class Tag(models.Model): name = models.CharField('Article tag',max_length=100) class Meta: verbose_name = 'Article tag' verbose_name_plural = verbose_name def __str__(self): return self.name #Recommended bit class Tui(models.Model): name = models.CharField('Recommended bit',max_length=100) class Meta: verbose_name = 'Recommended bit' verbose_name_plural = verbose_name def __str__(self): return self.name # Article class Article(models.Model): title = models.CharField('title', max_length=70) excerpt = models.TextField('abstract', max_length=200, blank=True) category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, verbose_name='classification', blank=True, null=True) # Using foreign key to associate classification table with classification is one to many relationship tags = models.ManyToManyField(Tag, verbose_name='label', blank=True) # Using foreign key to associate label table with label is many to many relationship img = models.ImageField(upload_to='article_img/%Y/%m/%d/', verbose_name='Article pictures', blank=True, null=True) body = UEditorField('content', width=800, height=500, toolbars="full", imagePath="upimg/", filePath="upfile/", upload_settings={"imageMaxSize": 1204000}, settings={}, command=None, blank=True ) user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='author') """ //Author of the article, here User is imported from django.contrib.auth.models. //Here we associate the article with User through ForeignKey. """ views = models.PositiveIntegerField('Reading volume', default=0) tui = models.ForeignKey(Tui, on_delete=models.DO_NOTHING, verbose_name='Recommended bit', blank=True, null=True) created_time = models.DateTimeField('Release time', auto_now_add=True) modified_time = models.DateTimeField('Modification time', auto_now=True) class Meta: verbose_name = 'Article' verbose_name_plural = 'Article' def __str__(self): return self.title #Banner class Banner(models.Model): text_info = models.CharField('title', max_length=50, default='') img = models.ImageField('Rotation chart', upload_to='banner/') link_url = models.URLField('pictures linking', max_length=100) is_active = models.BooleanField('Yes no active', default=False) def __str__(self): return self.text_info class Meta: verbose_name = 'Rotation chart' verbose_name_plural = 'Rotation chart' #Friendship link class Link(models.Model): name = models.CharField('Link name', max_length=20) linkurl = models.URLField('website',max_length=100) def __str__(self): return self.name class Meta: verbose_name = 'Friendship link' verbose_name_plural = 'Friendship link'
4. Register to write code in blog/models.py.
from django.contrib import admin from .models import Banner, Category, Tag, Tui, Article, Link #Import database tables to be managed @admin.register(Article) class ArticleAdmin(admin.ModelAdmin): list_display = ('id', 'category', 'title', 'tui', 'user', 'views', 'created_time') # Display the fields to be displayed in the article list list_per_page = 50 # Automatic pagination for 50 pieces of data ordering = ('-created_time',) #Sorting method of background data list list_display_links = ('id', 'title') # Set which fields can be clicked to enter the editing interface @admin.register(Banner) class BannerAdmin(admin.ModelAdmin): list_display = ('id', 'text_info', 'img', 'link_url', 'is_active') @admin.register(Category) class CategoryAdmin(admin.ModelAdmin): list_display = ('id', 'name', 'index') @admin.register(Tag) class TagAdmin(admin.ModelAdmin): list_display = ('id', 'name') @admin.register(Tui) class TuiAdmin(admin.ModelAdmin): list_display = ('id', 'name') @admin.register(Link) class LinkAdmin(admin.ModelAdmin): list_display = ('id', 'name','linkurl')
Error reporting when adding articles
Find that file and comment out line 96 to solve this error.
Try again. The function of adding articles is normal. The rich text editor is OK.