Wanna build your own blog with articles writing opportunity? Yes, its possible! Today we're build Django based blog application with WYSIWYG editor in Admin panel. It very fast for developing.
Create new Django project or open exist, then create application inside named as blog:
django-admin startapp blog
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=1024)
content = models.TextField(blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return '%s' % (self.title)
class Meta:
verbose_name = 'Article'
verbose_name_plural = 'Articles'
from django.contrib import admin
from .models import Article
class ArticleAdmin(admin.ModelAdmin):
list_display = ('id', 'title', 'created', 'updated')
search_fields = ('id', 'title', 'content', 'created', 'updated')
admin.site.register(Article, ArticleAdmin)
python manage.py makemigrations blog
python manage.py migrate
python manage.py runserver
Update blog/admin.py file with Summernote's assets from jsDelivr CDN:
from django.contrib import admin
from .models import Article
class ArticleAdmin(admin.ModelAdmin):
list_display = ('id', 'title', 'created', 'updated')
search_fields = ('id', 'title', 'content', 'created', 'updated')
class Media:
css = {
'all': [
'https://cdn.jsdelivr.net/npm/summernote@0.9.1/dist/summernote-lite.min.css',
'css/editor.css',
],
}
js = [
'https://cdn.jsdelivr.net/npm/summernote@0.9.1/dist/summernote-lite.min.js',
'admin/js/jquery.init.js',
'js/editor.js',
]
admin.site.register(Article, ArticleAdmin)
django.jQuery(function($) {
$('#id_content').summernote({
width: '100%',
height: 500
});
});