django安装DjangoUeditor富文本

环境:

pycharm,django1.11,python2.7

第一种:直接 pip install DjangoUeditor,直接从网上安装到pycharm

由于是直接安装,ueditor.html,ueditor_old.html会显示无法加载,请到项目下面查到这两个文件,把它拷到 templates 文件夹下面 ,这边是venv/DjangoUeditor\templates下面,大家根据各自的要求进行查看。其它model、urls、settings配置跟第二种一样,请直接看下面

第二种:

1.首先,在项目下面随便新建文件夹名ex_blog,

2.从https://files.pythonhosted.org/packages/92/78/6a97dabce8ab394c78c8ede4bd65a9d740685d65b942641e5859408af102/DjangoUeditor-1.8.143.zip,

下载安装包,解压包,把最里面的那个文件夹,DjangoUeditor拷到ex_blog下面,

3.通过pycharm 选中ex_blog文件夹,点击鼠标右键选中菜单 mark directory as 选择 sources root ,就可以变成上面的蓝色文件夹目录,(这一部分重要,用于把整个文件夹提升为项目可直接调用文件,关系到后面程序的调用)

4.在settings.py的INSTALLED_APPS中引入 DjangoUeditor,并文件中下面添加代码,如下 :

sys.path.insert(0, os.path.join(BASE_DIR, 'ex_blog')) 

并在最后添加图片上传路径

MEDIA_URL = '/static/uepload/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/uepload')

 5.在根的urls.py中添加(注意,并不是app中的urls.py)

url('^ueditor/', include('DjangoUeditor.urls'))

6.在models.py中引入UEditorField(注意:models模版中不可以对宽高进行设置)

from DjangoUeditor.models import UEditorField

例如:

class Blogblog(models.Model):
  content=UEditorField(verbose_name='内容')

7.在admin.py中注册,即可以在admin后台中使用富文本

from .models import Blogblog

class Blogblogadmin(admin.ModelAdmin):
  list_display = ['pk','content']

admin.site.register(Blogblog,Blogblogadmin)

注意:(第1种和第2种)由于django1.11都会报patterns的错误,请到DjangoUeditor目录下面urls.py中的patterns去除掉,换url,如下:

#coding:utf-8
from django import VERSION
if VERSION[0:2]>(1,3):
from django.conf.urls import  url
else:
from django.conf.urls.defaults import url

from views import get_ueditor_controller

urlpatterns = [
url(r'^controller/$',get_ueditor_controller)
]

 

HTML如何加载富文本

1.先在views.py中导入模块

from DjangoUeditor.forms import UEditorField
from django.forms import forms
from .models import Blogblog

2.定义一个forms(设置宽高,以及图片或文件上传的目录,根据上面设置是在uepload目录下面images和files)

class TestUEditorForm(forms.Form):
content = UEditorField('内容', width=600, height=300, toolbars="full", imagePath="images/", filePath="files/",upload_settings={"imageMaxSize":1204000},settings={})

3.把数据库内容渲染到页面

class Index(View):
def get(self,request):
form=TestUEditorForm()
blogIndex=Blogblog.objects.get(pk=1)
context=blogIndex.content
return render(request,'index.html',{'form':form,'context':context})

4.HTML页面的举例设置

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
{{ form.media }}    
</head>
<body>
{{ form }}
</body>
</html>

注意:{{ form.media }}    和 {{ form }}  都必须加载,才可以使用富文本

另外:如果数据库中加载出来的数据是html代码,

可以使用 {{ context|safe }} 直接转义.

或者使用

 

{% autoescape off %}
{{ context }}
{% endautoescape %}

 

转载于:https://www.cnblogs.com/weilaibuxiangshuo/p/10371931.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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 %},那么当您上传文件和图片时会失败
DjangoUeditorUeditor封装为一个Django app,提供文件/图片等上传功能,提供UEditorField、UEditorWidget等封装类,简化UeditorDjango中的应用。注:DjangoPython世界影响最大的Web框架。 本模块帮助在Django应用中集成百度Ueditor HTML编辑器,DjangoPython世界最有影响力的web框架。 Ueditor HTML编辑器是百度开源的在线HTML编辑器,功能非常强大,像表格可以直接拖动调整单元格大小等。 更新历史 [2014-7-8] Ver:1.8.143 Fix:当admin使用inlines生成多实例时widget命名不正确的问题 [2014-6-27] Ver:1.7.143 Fix:解决在admin管理后台的使用问题。 增加year,month,day的上传路径变量 [2014-6-25] 由于Ueditor从1.4版本开始,API发生了非常大的改动和不兼容,导致DjangoUeditor上一个版本的升级后上传功能不能用等,因此 本次重新设计了API,后端上传的代码几乎完全重写了。 更新到1.5.143,即版本号为1.5,使用了Ueditor 1.4.3版本。 重新设计了UeditorWidget、UeditorField。 新增了自定义Ueditor按钮的功能 注意:本次升级与之前版本不兼容,但是在使用体验上差别不大。 [2014-6-16] 更新到Ueditor 1.4.3 [2014-5-15] 增加不过滤 script,style ,不自动转div为p的脚本 修复在django 1.6和python2.7下的警告 使用 json 代替 django 中的 simplejson 用content_type 代替原来的 mime_type [2014-5-7] 更新到Ueditor 1.3.6 BUGfix:更新UEditor文件夹名字,避免在linux出现找不到静态文件问题 添加一种样式,besttome, 希望大家喜欢 [2013-2-22] 更新到Ueditor 1.2.5 BUGfix:更新UEditor文件夹名字,避免在linux出现找不到静态文件问题 BUGfix:现在支持south更新了 针对csrf导致上传图片失败的问题,现在默认上传视图开启了csrf_exempt装饰 相关阅读 百度编辑器UEditor插件DjangoUeditor使用方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值