使用DjangoUeditor富文本

不管是做项目管理系统,还是做网站,富本文是必用的,这里讲的是py2+django1.11,因为版本很重要。


1,安装富文本库:pip install DjangoUeditor

2,在项目的urls中添加:

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

url(r'^$', views.index),


如果报include问题,就导入include:from django.conf.urls import url, include

如果报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)

]

3,在项目的settings中加入项目DjangoUeditor:

INSTALLED_APPS = [

    'django.contrib.admin',

    'django.contrib.auth',

    'django.contrib.contenttypes',

    'django.contrib.sessions',

    'django.contrib.messages',

    'django.contrib.staticfiles',

    'web',

    'DjangoUeditor',

]

并在文件最后添加:

MEDIA_URL = '/static/uepload/'

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

4,定义models:

from DjangoUeditor.models import UEditorField

# 文章内容

class ArticleContent(models.Model):

    chapter_id = models.CharField('章节id', max_length=10)

    chapter_content = UEditorField(verbose_name=u'章节内容', default='')

5,添加到后台

import models

# 文章内容

class ArticleContentAdmin(admin.ModelAdmin):

    list_display = ('chapter_id', 'chapter_content')

    search_fields = ('chapter_content',)


admin.site.register(models.ArticleContent, ArticleContentAdmin)


到后台看效果如下:

1.png

6,在views中添加获取数据

def index(request):

# 获取文章内容

chapter_id = request.Get.get('id')

content_contacts = ArticleContent.objects.filter(id=chapter_id)

return render(request, 'index.html', { 'content_contacts': content_contacts})


7,前端页面显示富文本内容

{# 显示章节下的文章 #}

{% autoescape off %}

{% if content_contacts.count > 0 %}

{% for article_content in content_contacts %}

{{ article_content.chapter_content }}

{% endfor %}

{% endif %}

{% endautoescape %}


如果ueditor.html,ueditor_old.html显示无法加载,将Python27\Lib\site-packages\DjangoUeditor\templates目录下两个文件拷贝到项目 templates 目录


该文章对你有帮助吗,求分享转发: 分享到QQ空间 分享给QQ好友

 

 

粤ICP备19116230号
友情链接: 码农藏书阁 天天链