The Django framework is analyzed using the methods of the rich text editor Uedit

  • 2020-11-26 18:50:52
  • OfStack

This article illustrates how the Django framework USES the rich text editor Uedit. To share for your reference, the details are as follows:

Uedit is a very useful rich text editor for Baidu

1. Installation and basic configuration

Official GitHub (a detailed installation use tutorials) : https: / / github com/zhangfisher/DjangoUeditor

1. settings.py


INSTALLED_APPS = [
  ...
  'DjangoUeditor',
  ...
]

2. Configuration urls


from django.conf.urls import url, include
urlpatterns = [
#  Rich text correlation url
  url(r'^ueditor/', include('DjangoUeditor.urls')),
]

3. Field information

In models.py where the fields that need to use rich text are located


from DjangoUeditor.models import UEditorField
class Articles(models.Model):
  ...
  content = UEditorField(width=1200, height=600, imagePath="article/ueditor/",
              filePath="article/ueditor/",verbose_name=u" The article content ")
  ...

Note that in the class of adminx. py where the field to use ueditor is located, add


#  So that's what I'm specifying course the detail Field use ueditor Rich text editor 
class ArticlesAdmin(object):
  ...
  style_fields = {"content":"ueditor"}

2. Production of Ueditor plug-ins

1. Plug-in code

Create ueditor.py in ES44en_apps.xadmin.plugins


import xadmin
from xadmin.views import BaseAdminPlugin, CreateAdminView, ModelFormAdminView, UpdateAdminView
from DjangoUeditor.models import UEditorField
from DjangoUeditor.widgets import UEditorWidget
from django.conf import settings
class XadminUEditorWidget(UEditorWidget):
  def __init__(self, **kwargs):
    self.ueditor_options=kwargs
    self.Media.js = None
    super(XadminUEditorWidget, self).__init__(kwargs)
class UeditorPlugin(BaseAdminPlugin):
  def get_field_style(self, attrs, db_field, style, **kwargs):
    if style == 'ueditor':
      if isinstance(db_field, UEditorField):
        widget = db_field.formfield().widget
        param = {}
        param.update(widget.ueditor_settings)
        param.update(widget.attrs)
        return {'widget': XadminUEditorWidget(**param)}
    return attrs
  def block_extrahead(self, context, nodes):
    js = '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.config.js")
    js += '<script type="text/javascript" src="%s"></script>' % (settings.STATIC_URL + "ueditor/ueditor.all.min.js")
    nodes.append(js)
xadmin.site.register_plugin(UeditorPlugin, UpdateAdminView)
xadmin.site.register_plugin(UeditorPlugin, CreateAdminView)

2. Register plug-ins in xadmin

Add it in extra_apps.xadmin.plugins.___


PLUGINS = (
  ...
  'ueditor',
)

Friendship remind

Use the rich text editor in Django

In the HTML page, Django escaped the text content by default for security reasons, and we need to turn it off

To output our articles normally


{% autoescape off %}
{{ article.abstract }}
{% endautoescape %}

In record 1, the escape characters of Spaces are divided into the following types:

1. & nbsp; & 160 #; Continuous line whitespace (1 character width)

2. & ensp; & 8194 #; Half white space (1 character width)

3. & emsp; & 8195 #; 1 blank (2 character widths)

4. & thinsp; & 8201 #; Narrow white space (less than 1 character width)

Usually 1 used is & nbsp; But in Chinese it may be more appropriate sometimes & emsp;

I hope this article is helpful for Python programming based on Django framework.


Related articles: