转:Django使用Djangoueditor富文本编辑器

转自:https://blog.csdn.net/Mr_Sunqq/article/details/80041435

Django使用Djangoueditor富文本编辑器

——————————————————————————————————————

第一步:

- 下载Djangoueditor压缩包,将包解压放到项目目录文件夹下

- 下载地址:https://github.com/zhangfisher/DjangoUeditor

第二步:

- Python安装DjangoUeditor: pip install DjangoUeditor

第三步:

配置文件settings.py中注册DjangoUeditor

代码:

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01',
    'tinymce',
    'DjangoUeditor',
)

第四步:

配置DjangoUeditor相对应的url,在项目的主url文件中添加

代码:

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^ueditor/', include('DjangoUeditor.urls')),
]

第五步(后台使用):django自带后台admin使用

代码:

from DjangoUeditor.models import UEditorField

class News(models.Model):

    newscontent_djueditor = UEditorField(
        u'内容', height=100, width=500, default='test',
        toolbars='full'

    )

 

注意:DjangoUeditor需要使用到media路径的配置******(这个很重要)******

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_DIRS = (
    os.path.join(BASE_DIR, 'media')

)

第六步:

以上步骤实现完成后进入到django后台页面找到对应的表添加内容就能看到相应的djangoueditor编辑器的页面直接进行操作;

第七步(自建后台页面):我这里使用的是django的表单不是html的表单

代码:

from DjangoUeditor.forms import UEditorField
class DjangoueditorForm(forms.Form):
    content = UEditorField(
        "", initial="", width=800, height=200,
        toolbars='mini',
        imagePath='ueimages/',  # 上传图片存储的路径,不设置的话默认是配置文件中MEDIA_ROOT路径
        filePath='files_ue/'

    )

第八步(视图函数中添加业务逻辑):

代码:

@csrf_exempt
def addnews(request, nid):
    forms = DjangoueditorForm()
    return render(request, 'app01/addnews.html', {'forms': forms})

第九步(前端页面业务逻辑):

 

代码:

{% block forms_style %}

    {{ forms.media }}  {# 这里是加载djangoueditor相关的js、css文件,否则页面不显示富文本编辑器 #}
{% endblock %}

<form action="" method="post">

<div class="formRow">
    <label>内容:</label>
    <div class="formRight">
        {{ forms }}
    </div>
    <div class="clear"></div>

</div>

</form>

第十步:上传图片和文件后富文本编辑器Djangoueditor中图片和文件不显示的原因

代码:

# 添加配置文件这里如果不添加图片和文件不显示

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_DIRS = (
    os.path.join(BASE_DIR, 'media')

)

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ueditor HTML编辑器是百度开源的HTML编辑器, 本模块帮助在Django应用中集成百度Ueditor HTML编辑器。 安装包中已经集成Ueditor v1.2.2 使用Django-Ueditor非常简单,方法如下: 1、安装方法 **方法一:下载安装包,在命令行运行: python setup.py install **方法二:使用pip工具在命令行运行(推荐): pip install DjangoUeditor 2、在INSTALL_APPS里面增加DjangoUeditor app,如下: INSTALLED_APPS = ( #........ 'DjangoUeditor', ) 3、在urls.py中增加: url(r'^ueditor/',include('DjangoUeditor.urls' )), 4、在models中这样定义: from DjangoUeditor.models import UEditorField class Blog(models.Model): Name=models.CharField(,max_length=100,blank=True) Content=UEditorField('内容 ',height=100,width=500,default='test',imagePath="uploadimg/",imageManagerPath="imglib",toolbars='mini',options={"elementPathEnabled":True},filePath='upload',blank=True) 说明: UEditorField继承自models.TextField,因此你可以直接将model里面定义的models.TextField直接改成UEditorField即可。 UEditorField提供了额外的参数: toolbars:配置你想显示的工具栏,取值为mini,normal,full,代表小,一般,全部。如果默认的工具栏不符合您的要求,您可以在settings里面配置自己的显示按钮。参见后面介绍。 imagePath:图片上传的路径,如"images/",实现上传到"{{MEDIA_ROOT}}/images"文件夹 filePath:附件上传的路径,如"files/",实现上传到"{{MEDIA_ROOT}}/files"文件夹 imageManagerPath:图片管理器显示的路径,如"imglib/",实现上传到"{{MEDIA_ROOT}}/imglib",如果不指定则默认=imagepath。 options:其他UEditor参数,字典类型。参见Ueditor的文档ueditor_config.js里面的说明。 css:编辑器textarea的CSS样式 width,height:编辑器的宽度和高度,以像素为单位。 5、在表单中使用非常简单,与常规的form字段没什么差别,如下: class TestUeditorModelForm(forms.ModelForm): class Meta: model=Blog *********************************** 如果不是用ModelForm,可以有两种方法使用: 1: 使用forms.UEditorField from DjangoUeditor.forms import UEditorField class TestUEditorForm(forms.Form): Description=UEditorField("描述",initial="abc",width=600,height=800) 2: widgets.UEditorWidget from DjangoUeditor.widgets import UEditorWidget class TestUEditorForm(forms.Form): Content=forms.CharField(label="内容",widget=UEditorWidget(width=800,height=500, imagePath='aa', filePath='bb',toolbars={})) widgets.UEditorWidget和forms.UEditorField的输入参数与上述models.UEditorField一样。 6、Settings配置 在Django的Settings可以配置以下参数: UEDITOR_SETTINGS={ "toolbars":{ #定义多个工具栏显示的按钮,允行定义多个 "name1":[[ 'source', '|','bold', 'italic', 'underline']], "name2",[] }, "images_upload":{ "allow_type":"jpg,png", #定义允许的上传的图片类型 "max_size":"2222kb" #定义允许上传的图片大小,0代表不限制 }, "files_upload":{ "allow_type":"zip,rar", #定义允许的上传的文件类型 "max_size":"2222kb" #定义允许上传的文件大小,0代表不限制 },, "image_manager":{ "location":"" #图片管理器的位置,如果没有指定,默认跟图片路径上传一样 }, } 7、其他事项: **本程序基于百度ueditor 1.2.2,安装包里面已经包括了,不需要再额外安装。 **目前暂时不支持ueditor的插件 **Django默认开启了CSRF中间件,因此如果你的表单没有加入{% csrf_token %},那么当您上传文件和图片时会失败

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值