django 富文本

1.pip install django-ckeditor
2.pip list 查看是否注册
3.添加到app 'ckeditor'
4,设置model

from django.db import models
# Create your models here.
#from ckeditor.fields import RichTextField
from ckeditor_uploader.fields import RichTextUploadingField

class BlogType(models.Model):
  type_name=models.CharField(max_length=16)
  
  def __str__(self):
    return self.type_name
class Blog(models.Model):
  title=models.CharField(max_length=30)
  blog_type=models.ForeignKey(BlogType,on_delete=models.DO_NOTHING)
  content=RichTextUploadingField()
  author=models.CharField(max_length=20)
  created_time=models.DateField(auto_now_add=True)
  last_updated_time=models.DateTimeField(auto_now=True)
  class Meta:
    ordering=['-created_time']
  # on_delete=models.CASCADE,     # 删除关联数据,与之关联也删除
  #on_delete=models.DO_NOTHING,  # 删除关联数据,什么也不做

然后在admin中设置 使model与admin关联

from django.contrib import admin
# Register your models here.
from django.contrib import admin
from fu_text.models import *
admin.site.register(Blog)
admin.site.register(BlogType)

6.在setting 中设置 LANGUAGE_CODE = 'zh-hans',设置成中文

上传图片功能:


django 富文本_第1张图片
image.png

添加app:'ckeditor_uploader'
配置 settings

import sys
STATIC_URL = '/static/'
CKEDITOR_UPLOAD_PATH='content/ckeditor/'
#配置meida
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media') #自己在根目录下创建media文件夹
#自定义参数 配置ckeditor
CKEDITOR_UPLOADER_PAT='upload/'

4.设置URL:

#这是设置图片的上传路径

from django.conf import settings
from django.conf.urls.static import static 
urlpatterns = [
 url(r'^ckeditor',include('ckeditor_uploader.urls')) #这个是设计好的 不要改写
]
urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
  #settings.MEDIA_URL 想当于在setting设置的  /media/

5.在model中设置

#from ckeditor.fields import RichTextField
改成
from ckeditor_uploader.fields import RichTextUploadingField
content=RichTextUploadingField()

未完待续

你可能感兴趣的:(django 富文本)