如何把nicEditor集成到django中使用
2014-10-29 15:05
417 查看
http://blog.csdn.net/huyoo/article/details/4382317
我在我的ddtcms将会采用nicEditor作为表单中texteare的rich text editor
首先要,下载并解压nicEditor到media目录,我在media目录下建立了一个叫做editor的目录,下面放nicEditor或者tinymce之类的html编辑器
这里主要是介绍方法
然后就是建立一个NicEditor类,继承于Textarea(forms.Textarea),代码如下:
[python] view
plaincopy
class NicEditor(Textarea):
class Media:
js = ("%seditor/nicEdit/nicEdit.js" % settings.MEDIA_URL,)
def __init__(self, attrs=None):
self.attrs = {'class': 'niceditor'}
if attrs:
self.attrs.update(attrs)
super(NicEditor, self).__init__(attrs)
def render(self, name, value, attrs=None):
rendered = super(NicEditor, self).render(name, value, attrs)
context = {
'name': name,
'MEDIA_URL':settings.MEDIA_URL,
}
return rendered + mark_safe(render_to_string(
'niceditor/niceditor.html', context))
然后在你的某个app的forms.py 中使用它:
class CreateNewsForm(forms.Form):
title = forms.CharField(label=_("Title"), widget=forms.TextInput(attrs={'size': 50,'class': 'required'}), max_length=100,help_text="max_length is 100")
body = forms.CharField(label=_("Body"), widget=NicEditor(attrs={'rows':8, 'cols':50}))
可以看出,上面就是定义了一个widget(NicEditor),这个widget使用的是渲染模板弄的,模板文件是:niceditor/niceditor.html,内容如下:
<script type="text/javascript" src="{{MEDIA_URL}}editor/nicEdit/nicEdit.js"></script>
<script type="text/javascript">bkLib.onDomLoaded(function(){new nicEditor({maxHeight : 200}).panelInstance('id_{{name}}');});</script>
这上面的一段就是说把form中的一个name为id_body 的textare采用js处理后成为一个nicEditor
其他的forms.py中要使用nicEditor的地方都是按照上面说的弄
niceditor/niceditor.html可以放到templates目录下
可能需要应用的库是:
from django import forms
from django.utils.translation import ugettext as _
from django.forms import TextInput, Textarea
from django.conf import settings
from django.utils.safestring import mark_safe
from django.template import RequestContext
from django.template.loader import render_to_string
可以使用继承forms.Textarea的方法定义其他的editor,怎么渲染它就是你自己的事情了。对于一个基于jquery的editor:wymeditor来说,也可以使用同样的方法
上面的niceditor的一个背景图片要注意修改成正确的路径,可以修改niceditor的源码解决。
好了,就这些了,以后把代码发到google code吧.
我在我的ddtcms将会采用nicEditor作为表单中texteare的rich text editor
首先要,下载并解压nicEditor到media目录,我在media目录下建立了一个叫做editor的目录,下面放nicEditor或者tinymce之类的html编辑器
这里主要是介绍方法
然后就是建立一个NicEditor类,继承于Textarea(forms.Textarea),代码如下:
[python] view
plaincopy
class NicEditor(Textarea):
class Media:
js = ("%seditor/nicEdit/nicEdit.js" % settings.MEDIA_URL,)
def __init__(self, attrs=None):
self.attrs = {'class': 'niceditor'}
if attrs:
self.attrs.update(attrs)
super(NicEditor, self).__init__(attrs)
def render(self, name, value, attrs=None):
rendered = super(NicEditor, self).render(name, value, attrs)
context = {
'name': name,
'MEDIA_URL':settings.MEDIA_URL,
}
return rendered + mark_safe(render_to_string(
'niceditor/niceditor.html', context))
然后在你的某个app的forms.py 中使用它:
class CreateNewsForm(forms.Form):
title = forms.CharField(label=_("Title"), widget=forms.TextInput(attrs={'size': 50,'class': 'required'}), max_length=100,help_text="max_length is 100")
body = forms.CharField(label=_("Body"), widget=NicEditor(attrs={'rows':8, 'cols':50}))
可以看出,上面就是定义了一个widget(NicEditor),这个widget使用的是渲染模板弄的,模板文件是:niceditor/niceditor.html,内容如下:
<script type="text/javascript" src="{{MEDIA_URL}}editor/nicEdit/nicEdit.js"></script>
<script type="text/javascript">bkLib.onDomLoaded(function(){new nicEditor({maxHeight : 200}).panelInstance('id_{{name}}');});</script>
这上面的一段就是说把form中的一个name为id_body 的textare采用js处理后成为一个nicEditor
其他的forms.py中要使用nicEditor的地方都是按照上面说的弄
niceditor/niceditor.html可以放到templates目录下
可能需要应用的库是:
from django import forms
from django.utils.translation import ugettext as _
from django.forms import TextInput, Textarea
from django.conf import settings
from django.utils.safestring import mark_safe
from django.template import RequestContext
from django.template.loader import render_to_string
可以使用继承forms.Textarea的方法定义其他的editor,怎么渲染它就是你自己的事情了。对于一个基于jquery的editor:wymeditor来说,也可以使用同样的方法
上面的niceditor的一个背景图片要注意修改成正确的路径,可以修改niceditor的源码解决。
好了,就这些了,以后把代码发到google code吧.
相关文章推荐
- 如何把nicEditor集成到django中使用
- 如何把nicEditor集成到django中使用
- 如何使用eclipse集成的cvsc插件来协同工作?
- 在windows下使用apache与mod_python来集成django
- 如何独立使用django的数据库访问功能
- Django 当中使用CSS之类的东西如何设置路径
- visualsvn集成vs的插件如何使用新的repository
- cobertura 和tomcat如何集成使用
- django中如何使用login_required来解决登陆的麻烦
- django的用户认证管理,如何使用session(2)
- django的用户认证管理,如何使用session(1)
- django中如何使用tinymce
- 如何在Django 1.0中使用静态文件地址
- 在django如何使用中文
- ROJECT SERVER如何与OUTLOOK集成使用
- [ios开发基础之 Core Data[3]]如何使用NSFetchedResultsController 来集成TableView操作
- 如何在未加入域的机器上使用集成认证来登陆SQL Server
- Django 中如何使用css 详细解析
- django,如何安装和使用第三方apps
- PYTHON(DJANGO)使用日记之如何改变admin控制下的页面风格